-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11779.cpp
More file actions
61 lines (61 loc) · 1.48 KB
/
11779.cpp
File metadata and controls
61 lines (61 loc) · 1.48 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
#include <bits/stdc++.h>
#define X first
#define Y second
#define pii pair<int,int>
using namespace std;
int n, m;
vector<pii> adj[1001];
int dist[1001];
int info[1001][1001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for(int i = 0; i < 1001; i++) {
for(int j = 0; j < 1001; j++) info[i][j] = 123456789;
}
for(int i = 0; i < 1001; i++) dist[i] = 123456789;
cin >> n >> m;
while(m--) {
int a, b, c; cin >> a >> b >> c;
info[a][b] = min(info[a][b], c);
adj[a].push_back({c, b});
}
priority_queue<pii, vector<pii>, greater<pii>> pq;
int a, b; cin >> a >> b;
dist[a] = 0;
pq.push({0, a});
while(!pq.empty()) {
auto [cost, cur] = pq.top(); pq.pop();
if(cur == b) {
break;
}
if(dist[cur] < cost) continue;
for(auto [nxtCost, nxt] : adj[cur]) {
int c = cost + nxtCost;
if(dist[nxt] > c) {
dist[nxt] = c;
pq.push({c, nxt});
}
}
}
cout << dist[b] << "\n";
int cnt = 0;
int cur = b;
stack<int> ans;
ans.push(b);
while(cur != a) {
for(int i = 1; i <= n; i++) {
if(dist[i] + info[i][cur] == dist[cur]) {
ans.push(i);
cur = i;
break;
}
}
}
cout << ans.size() << "\n";
while(!ans.empty()) {
cout << ans.top() << " ";
ans.pop();
}
}