-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210308_BOJ_1753.cpp
More file actions
56 lines (43 loc) · 882 Bytes
/
Copy path210308_BOJ_1753.cpp
File metadata and controls
56 lines (43 loc) · 882 Bytes
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = 987654321;
int V, E;
int start;
vector<pair<int, int>> edge[20001];
vector<int> d(20001, INF);
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> V >> E;
cin >> start;
for (int i = 0; i < E; ++i) {
int u, v, w; cin >> u >> v >> w;
edge[u].push_back({w, v});
}
// Dijkstra
d[start] = 0;
priority_queue<pair<int, int>> pq;
pq.push({ 0, start });
while (!pq.empty()) {
int x = pq.top().second;
int w = pq.top().first * -1;
pq.pop();
for (int i = 0; i < edge[x].size(); ++i) {
int y = edge[x][i].second;
int sum = w + edge[x][i].first;
if (d[y] > sum) {
d[y] = sum;
pq.push({ sum * -1, y });
}
}
}
for (int i = 1; i <= V; ++i) {
if (d[i] == INF)
cout << "INF\n";
else
cout << d[i] << "\n";
}
return 0;
}