-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_1753.cpp
76 lines (61 loc) · 1.37 KB
/
BOJ_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
백준 1753 최단경로
21.06.03
https://github.com/skyqnaqna/algorithm_study
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <stack>
#define INF 1e9
#define endl "\n"
typedef long long ll;
typedef double dd;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
using namespace std;
vector<vector<pii> >edges;
int dist[20001];
int main()
{
int v, e, k; scanf("%d %d", &v, &e);
scanf("%d", &k);
fill(dist, dist + v + 1, INF);
edges.resize(v + 1);
for (int i = 0; i < e; ++i)
{
int u, v, w; scanf("%d %d %d", &u, &v, &w);
edges[u].push_back({-w, v});
}
priority_queue<pii> pq;
dist[k] = 0;
pq.push({0, k});
while (!pq.empty())
{
int now = pq.top().second;
int cost = -pq.top().first;
pq.pop();
if (dist[now] < cost) continue;
for (auto & edge : edges[now])
{
int next = edge.second;
int weight = -edge.first;
if (cost + weight < dist[next])
{
dist[next] = cost + weight;
pq.push({-dist[next], next});
}
}
}
for (int i = 1; i <= v; ++i)
{
if (dist[i] < INF) printf("%d\n", dist[i]);
else printf("INF\n");
}
return 0;
}