-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1916.cpp
More file actions
52 lines (38 loc) · 968 Bytes
/
BOJ_1916.cpp
File metadata and controls
52 lines (38 loc) · 968 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
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int>> adj[1001];
int ans[1001];
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
void dijkstra(int s){
pq.push({0,s});
ans[s] = 0;
while(!pq.empty()) {
int cur_cost = pq.top().first;
int cur_node = pq.top().second;
pq.pop();
if (cur_cost > ans[cur_node]) continue;
for (auto nxt : adj[cur_node]) {
int nxt_cost = nxt.first;
int nxt_node = nxt.second;
if (ans[nxt_node] > cur_cost + nxt_cost){
ans[nxt_node] = cur_cost + nxt_cost;
pq.push({ans[nxt_node], nxt_node});
}
}
}
}
int main(void) {
int N, M; //도시 개수 N(node), 버스 개수 M(edge)
cin >> N;
cin >> M;
fill(ans, ans + N + 1, INT_MAX);
for(int i = 1; i <= M; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({w, v});
}
int s, e;
cin >> s >> e;
dijkstra(s);
cout << ans[e] << "\n";
}