-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1916_최소비용_구하기.cpp
62 lines (47 loc) · 1.08 KB
/
1916_최소비용_구하기.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
#include <bits/stdc++.h>
#define INF INT_MAX
using namespace std;
int N;
vector<pair<int, int>> edges[1001];
int dijkstra(int s, int t)
{
vector<int> dijk(N + 1, INF);
priority_queue<pair<int, int>> pq;
pq.push({0, s});
dijk[s] = 0;
while (!pq.empty())
{
auto [distance, here] = pq.top();
distance *= -1;
pq.pop();
if (here == t)
break;
if (distance > dijk[here])
continue;
for (auto &[there, cost] : edges[here])
{
if (dijk[there] <= distance + cost)
continue;
dijk[there] = distance + cost;
pq.push({-dijk[there], there});
}
}
return dijk[t];
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int M;
cin >> N >> M;
for (int i = 0; i < M; i++)
{
int a, b, c;
cin >> a >> b >> c;
edges[a].emplace_back(b, c);
}
int s, t;
cin >> s >> t;
cout << dijkstra(s, t) << '\n';
return 0;
}