BOJ 링크
문제
코드
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
| #include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<pair<int, int>>graph[20001];
int cost[20001];
void dijkstra(int st)
{
priority_queue<pair<int, int>>pq;
pq.push({ 0, st });
cost[st] = 0;
while (!pq.empty())
{
int dist = -pq.top().first; // �켱����ť->��������
int now = pq.top().second;
pq.pop();
if (cost[now] < dist) continue;
for (int i = 0; i < graph[now].size(); i++)
{
int cal = dist + graph[now][i].second;
int targetNode = graph[now][i].first;
if (cal < cost[targetNode])
{
cost[targetNode] = cal;
pq.push(make_pair(-cal, targetNode));
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// input & init
int V, E, K;
cin >> V >> E >> K;
for (int i = 0; i < E; i++)
{
int u, v, w;
cin >> u >> v >> w;
graph[u].push_back({ v, w });
}
fill(cost, cost + 20001, 1e9);
// dijkstra algorithm
dijkstra(K);
// output
for (int i = 1; i <= V; i++)
{
if (cost[i] == 1e9)
{
cout << "INF\n";
}
else
{
cout << cost[i] << "\n";
}
}
return 0;
}
|
Leave a comment