-
Notifications
You must be signed in to change notification settings - Fork 0
/
1753.cpp
59 lines (55 loc) · 1.4 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
52
53
54
55
56
57
58
59
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int V, e, k;
vector<pair<int, int>>graph[20001];
int d[20001] = {0, };
int INF = 1000000;
void input();
void dijkstra();
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
dijkstra();
for(int i = 1; i <= V; i++){
if(d[i] == INF){
cout << "INF" << '\n';
continue;
}
cout << d[i] << '\n';
}
}
void input(){
cin >> V >> e;
cin >> k;
int u, v, w;
for(int i = 0; i < e; i++){
cin >> u >> v >> w;
graph[u].push_back({v, w});
}
}
void dijkstra(){
bool visited[20001] = {0, };
for(int i = 0; i <= V; i++){
d[i] = INF;
}
d[k] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;
pq.push({0, k});
while(!pq.empty()){
int currentDist = pq.top().first;
int currentNode = pq.top().second;
pq.pop();
if(visited[currentNode]==1)continue;
visited[currentNode] = 1;
for(int i = 0; i < graph[currentNode].size(); i++){
if(currentDist + graph[currentNode][i].second < d[graph[currentNode][i].first]){
d[graph[currentNode][i].first] = currentDist + graph[currentNode][i].second;
pq.push({d[graph[currentNode][i].first], graph[currentNode][i].first});
}
}
}
}