-
Notifications
You must be signed in to change notification settings - Fork 0
/
1753.cpp
51 lines (43 loc) · 1.02 KB
/
1753.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
#include<cstdio>
#include<utility>
#include<functional>
#include<vector>
#include<queue>
#define INF 987654321
using namespace std;
int V, E, start, u, v, w;
int dist[20001];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
vector<pair<int, int> > vec[20001];
void dijkstra() {
pq.push(make_pair(0, start));
dist[start] = 0;
while (!pq.empty()) {
int cost = pq.top().first;
int cur = pq.top().second;
pq.pop();
for (int i = 0; i < vec[cur].size(); i++) {
int next = vec[cur][i].first;
int n_cost = vec[cur][i].second;
if (dist[next] > cost + n_cost) {
dist[next] = cost + n_cost;
pq.push(make_pair(dist[next], next));
}
}
}
}
int main() {
scanf("%d %d", &V, &E);
scanf("%d", &start);
for (int i = 0; i < E; i++) {
scanf("%d %d %d", &u, &v, &w);
vec[u].push_back(make_pair(v, w));
}
for (int i = 1; i <= V; i++) dist[i] = INF;
dijkstra();
for (int i = 1; i <= V; i++) {
if (dist[i] == INF) printf("INF\n");
else printf("%d\n", dist[i]);
}
return 0;
}