#include using namespace std; #define each(i, c) for (auto& i : c) #define mkp(a, b) make_pair(a, b) typedef long long ll; typedef unsigned long long ull; typedef pair Pll; const ll MOD = 1e9+7; template ostream& operator << (ostream& os, pair p) { os << "(" << p.first << ": " << p.second << ")"; return os; } template ostream& operator << (ostream& os, vector v) { os << "("; each (i, v) os << i << ", "; os << ")"; return os; } template ostream& operator << (ostream& os, map m) { os << "{"; each (i, m) os << i << ", "; os << "}"; return os; } typedef struct edge { ll to; ll cost; } edge; typedef vector> G; // graph typedef vector GR; // graph result (cost, vertex num) GR dijkstra(G &g, ll s) { // O(ElogV) priority_queue, greater> q; GR res(g.size(), mkp(1e18, 0)); res[s] = mkp(0, s); q.push(res[s]); while (!q.empty()) { auto p = q.top(); q.pop(); ll cost = p.first; ll pos = p.second; if (res[pos].first < cost) continue; each (j, g[pos]) { ll to = j.to; ll to_cost = j.cost + cost; if (to_cost >= res[to].first) continue; res[to].first = to_cost; q.push(mkp(to_cost, to)); } } return res; } vector dijkstra_restore_path(G &g, GR &res, ll s, ll e) { ll from = e; vector path = {from}; while (from != s) { ll to = 1e18; each (i, g[from]) { if (res[from].first == res[i.to].first + i.cost) { to = min(to, i.to); } } from = to; path.push_back(from); } //reverse(path.begin(), path.end()); return path; } int main() { ll n, m, s, g; cin >> n >> m >> s >> g; G graph(n); for (ll i = 0; i < m; ++i) { ll a, b, c; cin >> a >> b >> c; graph[a].push_back(edge{.to = b, .cost = c}); graph[b].push_back(edge{.to = a, .cost = c}); } swap(s, g); auto res = dijkstra(graph, s); auto ans = dijkstra_restore_path(graph, res, s, g); for (ll i = 0; i < ans.size(); ++i) { cout << ans[i]; if (i == ans.size()-1) cout << endl; else cout << " "; } return 0; }