-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1753.cpp
More file actions
61 lines (47 loc) ยท 1.54 KB
/
BOJ_1753.cpp
File metadata and controls
61 lines (47 loc) ยท 1.54 KB
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
#include <bits/stdc++.h>
using namespace std;
/*
์ต์ํ ๋ง๋๋ ๋ฒ
ํ์์ ๊ฐ์ฅ ์์ ๊ฐ์ ๋จผ์ ์ฒ๋ฆฌํ๋ ค๋ฉด ์๋ ๊ฐ์์ ๋ถํธ๋ฅผ ๋ฐ๊ฟ์ ์์๋ก ์ ์ฅํ๋ฉด ๋จ.
์ด ์ฝ๋์ ๊ฒฝ์ฐ pair ๋ฅผ ์ฌ์ฉํ๋ฏ๋ก first๋ง -์ ๋๊ฐ
*/
priority_queue<pair<int,int>> pq;
vector<pair<int,int>> adj[20001]; //๊ฐ์ ๊ณผ ๊ฐ์ค์น ์ ์ฅ
int ans[300001]; //์ต๋จ ๊ฑฐ๋ฆฌ ํ
์ด๋ธ
const int INF = 1e9+10;
void dijkstra(int S) {
pq.push({0, S});
ans[S] = 0; //์์์ cost 0์ผ๋ก ์ธํ
// first = cost, second = ์ ์
while(!pq.empty()) {
int cur_cost = -pq.top().first; //์์๋ก ๋ง๋ค์ด์ ธ์์ผ๋๊น ์์๋ก ๋ค์ ๋ง๋ค๊ธฐ
int cur_edge = pq.top().second;
pq.pop();
if (cur_cost > ans[cur_edge]) continue; //๋จผ์ ํ์ธ
for (auto nxt : adj[cur_edge]) { //์ธ์ ๋
ธ๋ ์ํ
int next_cost = nxt.first;
int next_edge = nxt.second;
//๊ฐ์ค์น๊ฐ ๋ ์๊ฒ ๋์ค๋ฉด ์
๋ฐ์ดํธ
if (ans[next_edge] > cur_cost + next_cost) {
ans[next_edge] = cur_cost + next_cost;
pq.push({-ans[next_edge], next_edge}); //
}
}
}
}
int main(void) {
int V, E, S; //์ ์ ์ ๊ฐ์ V, ๊ฐ์ ์ ๊ฐ์ E, ์์์ S
cin >> V >> E;
cin >> S;
fill(ans, ans+V+1, INF); //INFINITE๋ก ์ด๊ธฐํ
for (int i = 1; i <= E; i++){
int u, v, w; //u์์ v๋ก ๊ฐ๋ ๊ฐ์ค์น w
cin >> u >> v >> w;
adj[u].push_back({w,v});
}
dijkstra(S);
for (int i = 1; i <= V; i++) {
if(ans[i] == INF) cout << "INF\n";
else cout << ans[i] << "\n";
}
}