-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra-Algorithm.cpp
68 lines (52 loc) · 1.43 KB
/
Dijkstra-Algorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/// @author mann2108
/// DIJKSTRA'S Algorithm (SSSP)
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define push_back pb
int main(){
ll t;
cin>>t;
while(t--){
double n,m,src,des;
cin>>n>>m>>src>>des;
vector<vector<pair<ll,double> > > adj;
vector<pair<ll,double> > vec;
for(ll i=0;i<=n;i++)adj.push_back(vec);
double u,v,len,sp;
while(m--){
cin>>u>>v>>len>>sp;
adj[u].push_back({v,(double)((double)len/(double)sp)});
adj[v].push_back({u,(double)((double)len/(double)sp)});
}
set<pair<double,ll> > q;
vector<double> d(n+1,(double)LLONG_MAX);
vector<ll> p(n+1,-1);
q.insert({0.0,src});
d[src]=0.0;
while(!q.empty()){
ll v=q.begin()->second;
if(v==des){
break;
}
q.erase(q.begin());
for(auto edge:adj[v]){
ll to=edge.first;
double len=edge.second;
// Relaxation
if(d[v]+len<d[to]){
q.erase({d[to],to});
d[to]=d[v]+len;
p[to]=v;
q.insert({d[to],to});
}
}
}
if(d[des]==LLONG_MAX){
cout<<"-1"<<endl;
}
else{
cout<<fixed<<setprecision(8)<<d[des]<<endl;
}
}
}