-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj1753
More file actions
51 lines (39 loc) · 879 Bytes
/
boj1753
File metadata and controls
51 lines (39 loc) · 879 Bytes
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 <stdio.h>
#include <vector>
#include <queue>
#define INF 987654321
using namespace std;
vector<pair<int,int> > node[20005];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pq;
int value[20005];
int main(){
int n,e,k;
int u,v,w;
scanf("%d %d",&n,&e);
scanf("%d",&k);
for(int i=0;i<e;i++){
scanf("%d %d %d",&u,&v,&w);
node[u].push_back(make_pair(v,w));
}
for(int i=1;i<=n;i++)
value[i] = INF;
value[k] = 0;
pq.push(make_pair(0,k));
while(!pq.empty()){
int x = pq.top().first;
int U = pq.top().second;
pq.pop();
for(int i=0;i<node[U].size();i++){
int V = node[U][i].first;
int W = node[U][i].second;
if(x+W<value[V]){
value[V] = x+W;
pq.push(make_pair(x+W,V));
}
}
}
for(int i=1;i<=n;i++)
if(value[i]==INF) printf("INF\n");
else printf("%d\n",value[i]);
return 0;
}