#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int N = 1e3 + 10;
const int M = 1e6 + 10;
const int inf = 0x3f3f3f3f;
int n, m, idx, h[N], e[M], ne[M], w[M], dist[N], st, ed;
bool book[N];
vector<int> lst;
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dij() {
memset(dist, 0x3f, sizeof dist);
priority_queue<pii, vector<pii>, greater<pii>> q;
q.push({0, st});
dist[st] = 0;
while( q.size() ) {
auto [d, now] = q.top();
q.pop();
if(book[now]) continue;
book[now] = true;
for(int i=h[now]; ~i; i=ne[i]) {
int j = e[i], k = w[i];
if(d + k < dist[j]) {
dist[j] = d + k;
q.push({dist[j], j});
if(j == ed) {
lst.clear();
lst.emplace_back(now);
}
} else if(d + k == dist[j] && j == ed) {
lst.emplace_back(now);
}
}
}
}
int main() {
ios :: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
memset(h, -1, sizeof h);
while( m -- ) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
cin >> ed >> st;
if(st == ed) {
cout << 0 << '\n' << -1;
return 0;
}
dij();
if(dist[ed] != inf) {
cout << dist[ed] << '\n';
for(auto &num : lst) cout << num << ' ';
} else {
cout << -1 << '\n' << -1;
}
return 0;
}
// 64 位输出请用 printf("%lld")
从终点向起点做一遍dijkstra算法,同时维护一下可以更新起点最短距离的上一个点集即可



京公网安备 11010502036488号