-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1753.cpp
43 lines (34 loc) · 873 Bytes
/
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
#include <iostream>
#include <vector>
#include <queue>
#define INF 0x7FFFFFFF
using namespace std;
vector<pair<int,int>> graph[20001];
int V, E, K, u, v, w;
priority_queue<pair<int,int>> pq;
int weight[20001];
int main(){
cin >> V >> E >> K;
for(int i = 1; i <= V; i++) weight[i] = INF;
weight[K] = 0;
for(int i = 0; i < E; i++) {
cin >> u >> v >> w;
graph[u].push_back({v,w});
}
pq.push({0,K});
while(!pq.empty()) {
int cost = pq.top().first;
int pos = pq.top().second;
pq.pop();
for(pair<int,int> next : graph[pos]) {
int cost_next = next.second;
int pos_next = next.first;
if (-cost + cost_next < weight[pos_next]) {
weight[pos_next] = -cost + cost_next;
pq.push({-weight[pos_next], pos_next});
}
}
}
for(int i = 1; i <= V; i++) if(weight[i] != INF) cout << weight[i] << "\n"; else cout << "INF\n";
return 0;
}