#include using namespace std; using ll = long long; struct State { int at; ll cost; int prev; State(int at, ll cost, int prev) : at(at), cost(cost), prev(prev) {} bool operator>(const State& s) const { if (cost != s.cost) return cost > s.cost; if (prev != s.prev) return prev > s.prev; //最短経路を辞書順最小にする(省略可) return at > s.at; //return cost > s.cost; } }; struct Edge { int to; ll cost; Edge(int to, ll cost) : to(to), cost(cost) {} }; using Graph = vector >; //隣接リスト const ll INF = 1e15; const int NONE = -1; //sは始点、mincは最短経路のコスト、prevsは最短経路をたどる際の前の頂点 void dijkstra(int s, const Graph& graph, vector& minc, vector& prevs){ minc.assign(graph.size(), INF); prevs.assign(graph.size(), NONE); priority_queue, greater > pq; pq.emplace(s, 0, NONE); while(!pq.empty()) { State cur = pq.top(); pq.pop(); //if (minc[cur.at] < cur.cost) continue; if (minc[cur.at] < INF) continue; minc[cur.at] = cur.cost; prevs[cur.at] = cur.prev; for(const Edge& e : graph[cur.at]) { ll cost = cur.cost + e.cost; if (minc[cur.at] == INF) continue; //if (minc[e.to] < cost || minc[e.to] == cost && prevs[e.to] <= cur.at) continue; //minc[e.to] = cost; //prevs[e.to] = cur.at; pq.emplace(e.to, cost, cur.at); } } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m, s, g; cin >> n >> m >> s >> g; Graph graph(n); for (int i = 0; i < m; i++) { int a, b; ll c; cin >> a >> b >> c; graph[a].emplace_back(b, c); graph[b].emplace_back(a, c); } vector minc; vector prevs; dijkstra(s, graph, minc, prevs); vector ans; int cur = g; while (cur != NONE) { ans.push_back(cur); cur = prevs[cur]; } reverse(ans.begin(), ans.end()); int sz = ans.size(); for (int i = 0; i < sz; i++) { cout << ans[i] << " \n"[i + 1 == sz]; } return 0; }