-
Notifications
You must be signed in to change notification settings - Fork 0
/
boj1753.cpp
62 lines (48 loc) · 1.09 KB
/
boj1753.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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 1e9
using namespace std;
int V, E;
int st;
vector<pair<int, int>>* edge;
int* dist;
void dijkstra(int st) {
priority_queue<pair<int, int>> pq;
pq.push({ 0, st });
dist[st] = 0;
while (!pq.empty()) {
int d = -pq.top().first;
int now = pq.top().second;
pq.pop();
if (dist[now] < d) continue;
for (int i = 0; i < edge[now].size(); i++) {
int cost = d + edge[now][i].second;
if (cost < dist[edge[now][i].first]) {
dist[edge[now][i].first] = cost;
pq.push({-cost, edge[now][i].first });
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> V >> E;
cin >> st;
edge = new vector<pair<int, int>>[V+1];
dist = new int[V+1];
int u, v, w;
for (int i = 1; i <= V; i++) {
dist[i] = INF;
}
for (int i = 0; i < E; i++) {
cin >> u >> v >> w;
edge[u].push_back(make_pair(v, w));
}
dijkstra(st);
for (int i = 1; i <= V; i++) {
if (dist[i] == INF) cout << "INF\n";
else cout << dist[i] <<'\n';
}
}