fork download
  1. #include<iostream>
  2. #include<vector>
  3. #include<queue>
  4. #define ll long long
  5.  
  6. using namespace std;
  7. struct node{
  8. ll v;
  9. double w;
  10. };
  11. struct comp{
  12. bool operator()(const node &a,const node &b) const
  13. {
  14. return a.w>b.w;
  15. }
  16. };
  17. float findMin(vector<pair<ll,double>> adj[],bool visited[],double dist[],ll a,ll b){
  18. dist[a] = 0;
  19. priority_queue<node,vector<node>,comp> q;
  20. q.push({a,0});
  21. visited[a] = true;
  22. while(!q.empty()){
  23. node p = q.top();
  24. q.pop();
  25. for(auto x:adj[p.v]){
  26. if(!visited[x.first]&&dist[x.first]> dist[p.v] + x.second){
  27. dist[x.first] = dist[p.v] + x.second;
  28. q.push({x.first,dist[x.first]});
  29. }
  30. }
  31. visited[p.v] = true;
  32. }
  33. if(visited[b])
  34. return dist[b];
  35. else return -1;
  36. }
  37. int main(){
  38. int t;
  39. cin >>t;
  40. while(t--){
  41. ll n,m,a,b,i,x,y,d,s;
  42. cin >> n>>m>>a>>b;
  43. vector<pair<ll,double>> adj[n+1];
  44. bool visited[n+1];
  45. double dist[n+1];
  46. for(i=0;i<=n;i++){
  47. visited[i] = false;
  48. dist[i] = 1e18;
  49. }
  50. for(i=0;i<m;i++){
  51. cin >>x>>y>>d>>s;
  52. adj[x].push_back(make_pair(y,(double)d/s));
  53. adj[y].push_back(make_pair(x,(double)d/s));
  54. }
  55. cout <<findMin(adj,visited,dist,a,b)<<endl;
  56. }
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0s 4532KB
stdin
1            
5 6 1 5            
1 2 7 4            
1 4 10 2            
2 3 5 2            
4 3 4 3            
3 5 2 1            
4 5 8 5
stdout
6.25