-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaekjoon_#1753_practice.cpp
More file actions
53 lines (46 loc) · 978 Bytes
/
Baekjoon_#1753_practice.cpp
File metadata and controls
53 lines (46 loc) · 978 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
52
53
#include<bits/stdc++.h>
using namespace std;
#define MAXN 2000001
#define INF 97654321
struct Data {
int node,weight;
Data(){};
Data(int node,int weight) {};
bool operator<(const Data d) const {
return weight>d.weight;
}
};
int V,E,K,a,b;
bool visited[MAXN];
int cost[MAXN];
vector<Data> v[MAXN];
priority_queue<Data> pq;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin>>V>>E>>K;
for(int i=0;i<E;i++) {
cin>>a>>b>>w;
v[a].push_back(Data(b,w));
}
for(int i=1;i<=V;i++) cost[i] =INF;
cost[K] = 0;
pq.push(Data(K,0));
Data curr,next;
while(!pq.empty()) {
curr = pq.top();
pq.pop();
if(visited[curr]) continue;
visited[curr] = true;
for(int i=0;i<v[curr.node].size();i++) {
next = v[curr.node].at(i);
cost[next.node] = min(cost[next.node],cost[curr.node]+next.weight);
pq.push(Data(next.node,cost[next.node]));
}
}
for(int i=1;i<=V;i++) {
if(cost[i]==INF) cout<<"INF\n";
else cout<<cost[i]<<'\n';
}
return 0;
}