-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1916.cpp
62 lines (43 loc) · 1.05 KB
/
P1916.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 <iostream>
#include <vector>
#include <queue>
#define INF 987654321
using namespace std;
typedef pair<int, int> pii;
int N, M, START, END;
vector<pii> graph[100001];
vector<int> dijk() {
vector<int> dis(N, INF);
priority_queue<pii> pq;
dis[START] = 0;
pq.push({0, START});
while (!pq.empty()) {
int cost = -pq.top().first;
int here = pq.top().second;
pq.pop();
if (dis[here] < cost) continue;
for (int i = 0; i < graph[here].size(); i++) {
int ncost = cost + graph[here][i].second;
int n = graph[here][i].first;
if (dis[n] < ncost) continue;
dis[n] = ncost;
pq.push({-ncost, n});
}
}
return dis;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> N >> M;
for (int i = 0; i < M; i++) {
int a, b, c;
cin >> a >> b >> c;
graph[a].emplace_back( b, c );
}
N++;
cin >> START >> END;
cout << dijk()[END];
return 0;
}