-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.cpp
69 lines (53 loc) · 1.31 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
// BOJ 1753 최단경로
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
struct Node {
ll x, d;
bool operator<(const Node &o) const { return d > o.d; }
};
vector<ll> dijkstra(int start, vector<vector<pll>> &graph) {
vector<ll> dist(graph.sz, LLONG_MAX);
dist[start] = 0;
priority_queue<Node> pq;
pq.push({start, 0});
while (pq.sz) {
Node cur = pq.top();
pq.pop();
if (dist[cur.x] != cur.d)
continue;
for (pll &nxt : graph[cur.x]) {
if (cur.d + nxt.se < dist[nxt.fi]) {
dist[nxt.fi] = cur.d + nxt.se;
pq.push({nxt.fi, dist[nxt.fi]});
}
}
}
return dist;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, start;
cin >> n >> m >> start;
vector<vector<pll>> graph(n + 1);
while (m--) {
ll u, v, w;
cin >> u >> v >> w;
graph[u].push_back(pll(v, w));
}
vector<ll> dist = dijkstra(start, graph);
for (int i = 1; i <= n; i++) {
if (dist[i] == LLONG_MAX)
cout << "INF" << '\n';
else
cout << dist[i] << '\n';
}
}