#include using namespace std; typedef long long ll; struct node { int at; ll cost; //オーバーフローを避けるためlong longに int prev; node(int at, ll cost, int prev) : at(at), cost(cost), prev(prev) {} bool operator>(const node &s) const { if (cost != s.cost) return cost > s.cost; if (prev != s.prev) return prev > s.prev; return at > s.at; } }; struct Edge { int to; ll cost; Edge(int to, ll cost) : to(to), cost(cost) {} }; typedef vector > AdjList; //隣接リスト typedef vector::iterator Edge_it; const ll INF = 100000000000; const int NONE = 1000; AdjList graph; vector minc; //最短経路のコスト vector Prev; //最短経路をたどる際の前の頂点 void dijkstra(int n, int s){ //nは頂点数、sは始点 minc = vector(n, INF); Prev = vector(n, NONE); priority_queue, greater > pq; pq.push(node(s, 0, NONE)); while(!pq.empty()) { node x = pq.top(); pq.pop(); if (minc[x.at] != INF) continue; minc[x.at] = x.cost; Prev[x.at] = x.prev; for(Edge_it i = graph[x.at].begin(), e = graph[x.at].end(); i != e; ++i) { if (minc[(*i).to] == INF) { pq.push(node((*i).to, x.cost + (*i).cost, x.at)); } } } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m, s, g; cin >> n >> m >> s >> g; graph = AdjList(n); for(int i = 0; i < m; i++) { int from, to; ll cost; cin >> from >> to >> cost; graph[from].push_back(Edge(to, cost)); graph[to].push_back(Edge(from, cost)); } dijkstra(n, g); int tmp = s; while(tmp != g){ cout << tmp << " "; tmp = Prev[tmp]; } cout << g << "\n"; return 0; }