-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicpc1753.cpp
More file actions
48 lines (45 loc) · 1.17 KB
/
icpc1753.cpp
File metadata and controls
48 lines (45 loc) · 1.17 KB
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
#include <iostream>
#include <vector>
#include <queue>
#include <list>
using namespace std;
// first -> d, second -> v
using Edge = pair<int, int>;
void dijkstra(int s, vector<int> &dist, vector<list<Edge> > &graph){
priority_queue<Edge, vector<Edge>, greater<Edge> > pq;
dist[s] = 0;
pq.push(Edge(dist[s], s));
while(pq.size()){
int u = pq.top().second;
pq.pop();
for(Edge edge : graph[u]){
int d = edge.first, v = edge.second;
if(dist[v] != -1 && dist[v] <= dist[u] + d) continue;
dist[v] = dist[u] + d;
pq.push(Edge(dist[v], v));
}
graph[u].clear();
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int v, e, k;
vector<int> dist;
vector<list<Edge> > graph;
cin >> v >> e >> k;
dist.resize(v + 1, -1);
graph.resize(v + 1);
for(int i = 0; i < e; i++){
int u, v, d;
cin >> u >> v >> d;
graph[u].push_back(Edge(d, v));
}
dijkstra(k, dist, graph);
for(int i = 1; i <= v; i++)
if(dist[i] < 0)
cout << "INF\n";
else
cout << dist[i] << '\n';
}