forked from mazedrupok/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Dijkstra algorithm
53 lines (51 loc) · 1.15 KB
/
Dijkstra algorithm
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
#include <bits//stdc++.h>
using namespace std;
#define mx 20010
vector<int> edge[mx], cost[mx];
const int infinity = 1000000000;
struct data {
int city, dist;
bool operator < ( const data& p ) const {
return dist > p.dist;
}
};
int dijkstra(int source, int destination) {
int d[mx];
for(int i=0; i<=mx; i++) d[i] = infinity;
priority_queue<data> q;
data u, v;
u.city = source, u.dist = 0;
q.push(u);
d[source] = 0;
while(!q.empty()) {
u = q.top(); q.pop();
int ucost = d[u.city];
for(int i=0; i<edge[u.city].size(); i++) {
v.city = edge[u.city][i], v.dist = cost[u.city][i] + ucost;
// relaxing :)
if(d[v.city] > v.dist) {
d[v.city] = v.dist;
q.push( v );
}
}
}
return d[destination];
}
int main()
{
int n, e, src, dist;
while(scanf("%d %d", &n, &e)==2) {
while(e--) {
int u, v, w;
cin>>u>>v>>w;
edge[u].push_back(v);
edge[v].push_back(u);
cost[u].push_back(w);
cost[v].push_back(w);
}
cin>>src>>dist;
int ans = dijkstra(src, dist);
cout<<ans<<endl;
}
return 0;
}