#include #include #include #include #include using namespace std; template struct Edge { int src, dst; T cost; Edge(int dst, T cost) : src(-1), dst(dst), cost(cost) { } Edge(int src, int dst, T cost) : src(src), dst(dst), cost(cost) { } }; template using Edges = vector>; template using WeightedGraph = vector>; template using Matrix = vector>; template Matrix warshall_floyd(Matrix &g) { Matrix d(g); const T INF = numeric_limits::max(); for (int k = 0; k < d.size(); k++) { for (int i = 0; i < d.size(); i++) if (d[i][k] != INF) { for (int j = 0; j < d.size(); j++) if (d[k][j] != INF) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } return d; } template vector build_path(Matrix &mat, Matrix &dist, int s, int t) { vector path{s}; while (s != t) { for (int k = 0; k < mat.size(); k++) if (s != k) { if (mat[s][k] + dist[k][t] == dist[s][t]) { path.emplace_back(k); s = k; break; } } } return path; } int main() { const int INF = numeric_limits::max(); int N, M, S, G; cin >> N >> M >> S >> G; Matrix mat(N, vector(N, INF)); for (int i = 0; i < N; i++) mat[i][i] = 0; while (M--) { int a, b, c; cin >> a >> b >> c; mat[a][b] = mat[b][a] = c; } Matrix dist = warshall_floyd(mat); for (int v: build_path(mat, dist, S, G)) cout << v << " \n"[v == G]; return 0; }