#include #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 struct Dijkstra { const T INF = numeric_limits::max(); const WeightedGraph &g; int V, s; vector dist; vector prev; Matrix mat; Dijkstra(const WeightedGraph &g, int s) : g(g), V(g.size()), s(s) { mat.assign(V, vector(V)); // MLE に注意 for (int u = 0; u < V; u++) for (auto e: g[u]) mat[u][e.dst] = e.cost; dist.assign(V, INF); prev.assign(V, -1); using Pi = pair; priority_queue, greater> que; dist[s] = 0; que.emplace(dist[s], s); while (!que.empty()) { T cost; int u; tie(cost, u) = que.top(); que.pop(); if (dist[u] < cost) continue; for (auto &e: g[u]) { int v = e.dst; T nc = e.cost; if (dist[v] < dist[u] + nc) continue; if (dist[v] == dist[u] + nc && prev[v] != -1) { if (prev[v] > u) prev[v] = min(prev[v], u); else continue; } else { prev[v] = u; } dist[v] = dist[u] + nc; que.emplace(dist[v], v); } } } vector build_path(int t) { vector path; for (int u = t; u >= 0; u = prev[u]) path.emplace_back(u); //reverse(path.begin(), path.end()); return path; } void dump_path(int t) { dump_path(build_path(t)); } void dump_path(const vector &path) { int t = path.back(); cerr << s << " -> " << t << " (d = "; if (dist[t] == INF) cerr << "∞): "; else cerr << dist[t] << "): "; for (auto it = path.begin(); it != path.end() - 1; it++) cerr << *it << " -[" << mat[*it][*(it + 1)] << "]-> "; if (s == path.front()) cerr << t; cerr << endl; } }; int main() { int N, M, S, G; cin >> N >> M >> S >> G; WeightedGraph g(N); while (M--) { int a, b, c; cin >> a >> b >> c; g[a].emplace_back(b, c); g[b].emplace_back(a, c); } Dijkstra sssp(g, G); int i = 0; for (int x: sssp.build_path(S)) { if (i++) cout << " "; cout << x; } cout << endl; return 0; }