-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11779.cpp
More file actions
65 lines (59 loc) · 1.5 KB
/
11779.cpp
File metadata and controls
65 lines (59 loc) · 1.5 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
62
63
64
65
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
vector<pair<int, int>> adj[1001];
vector<int> trace(1001, 0);
vector<int> dijkstra(int n, int start) {
vector<int> dist(n + 1, INF);
priority_queue<pair<int, int>> pq;
dist[start] = 0;
pq.push({start, 0});
while (!pq.empty()) {
int cur = pq.top().first;
int cur_dist = -pq.top().second;
pq.pop();
if (dist[cur] < cur_dist) continue;
for (int i = 0; i < adj[cur].size(); i++) {
int next = adj[cur][i].first;
int next_dist = cur_dist + adj[cur][i].second;
if (next_dist < dist[next]) {
trace[next] = cur;
dist[next] = next_dist;
pq.push({next, -next_dist});
}
}
}
return dist;
}
vector<int> get_path(int last) {
vector<int> path;
while (last) {
path.push_back(last);
last = trace[last];
}
reverse(path.begin(), path.end());
return path;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
int from, to;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
}
cin >> from >> to;
vector<int> dist = dijkstra(n, from);
cout << dist[to] << "\n";
vector<int> path = get_path(to);
cout << path.size() << "\n";
for (int i : path) {
cout << i << " ";
}
}