-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode20C.cpp
More file actions
61 lines (51 loc) · 1.1 KB
/
Copy pathcode20C.cpp
File metadata and controls
61 lines (51 loc) · 1.1 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
/*
* Contest : Codeforces
* Problem : 20C - Dijkstra?
* Link : https://codeforces.com/problemset/problem/20/C
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
int main() {
fastio
ll n, m;
cin >> n >> m;
vector<pair<ll, ll>> graph[n + 1];
vector<ll> dist(n + 1, 1e18);
vector<ll> child(n + 1, -1);
while (m--) {
ll a, b, c;
cin >> a >> b >> c;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
priority_queue<pair<ll, ll>> pq;
dist[1] = 0;
pq.push({0, 1});
while (!pq.empty()) {
auto u = pq.top().second;
pq.pop();
for (auto &[v, w] : graph[u]) {
if (dist[u] + w < dist[v]) {
child[v] = u;
dist[v] = dist[u] + w;
pq.push({-dist[v], v});
}
}
}
if (dist[n] == 1e18) {
cout << "-1\n";
}
else {
vector<ll> path;
for (auto i = n;; i = child[i]) {
if (i == -1) break;
path.push_back(i);
}
reverse(path.begin(), path.end());
for (auto &i : path) cout << i << ' ';
cout << '\n';
}
return 0;
}