-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdijkstra.cpp
74 lines (63 loc) · 1.7 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
// MOVED
// iran
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
class Dijkstra {
int n;
vector<vector<pair<int, double>>> adj;
vector<double> d;
public:
Dijkstra(int n) : n(n) {
adj = vector<vector<pair<int, double>>>(n);
d = vector<double>(n, numeric_limits<double>::infinity());
}
void addEdge(int a, int b, double w) {
assert(0 <= a && a < n && 0 <= b && b < n);
adj[a].push_back(make_pair(b, w));
adj[b].push_back(make_pair(a, w));
}
void addArc(int a, int b, double w) {
assert(0 <= a && a < n && 0 <= b && b < n);
adj[a].push_back(make_pair(b, w));
}
void run(int a) {
d[a] = 0;
priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> q;
q.push(make_pair(0, a));
while (!q.empty()) {
auto top = q.top();
int u = top.second;
q.pop();
if (d[u] < top.first) continue;
for (auto i: adj[u]) {
if (d[i.first] > d[u] + i.second) {
d[i.first] = d[u] + i.second;
q.push(make_pair(d[i.first], i.first));
}
}
}
}
double dist(int a) {
return d[a];
}
};
int main() {
int v, e, r;
cin >> v >> e >> r;
Dijkstra g(v);
for (int i = 0; i < e; i++) {
int a, b, w;
cin >> a >> b >> w;
g.addArc(a, b, w);
}
g.run(r);
for (int i = 0; i < v; i++) {
double d = g.dist(i);
if (d == numeric_limits<double>::infinity()) {
cout << "INF" << endl;
} else {
cout << i64(d) << endl;
}
}
}