-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra.cpp
75 lines (62 loc) · 1.2 KB
/
Dijkstra.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
69
70
71
72
73
74
75
#include<bits/stdc++.h>
using namespace std;
#define INF (1<<28)
struct node
{
int u;
int cost;
bool operator <(const node &a) const
{
return cost > a.cost;
}
};
priority_queue <node> PQ;
vector <node> vec[100];
int nodes, edges, dist[100];
void dijkstra(int start)
{
node u, v;
int uu, vv, cst;
v.u = start;
v.cost = dist[start];
PQ.push(v);
while(PQ.size())
{
u = PQ.top();
PQ.pop();
uu = u.u;
for(int i = 0; i < vec[uu].size(); ++i)
{
vv = vec[uu][i].u;
cst = vec[uu][i].cost;
if(dist[uu]+cst < dist[vv])
{
dist[vv] = dist[uu]+cst;
v.u = vv;
v.cost = dist[vv];
PQ.push(v);
}
}
}
return;
}
int main()
{
int a, b, c;
node v;
cin >> nodes >> edges;
for(int i = 1; i <= nodes; ++i)
dist[i] = INF;
for(int i = 1; i <= edges; ++i)
{
cin >> a >> b >> c;
v.u = b;
v.cost = c;
vec[a].push_back(v);
}
dist[1] = 0;
dijkstra(1);
for(int i = 1; i <= nodes; ++i)
cout << dist[i] << " ";
return 0;
}