#define _CRT_SECURE_NO_WARNINGS //#define _GLIBCXX_DEBUG #include using namespace std; typedef long long ll; //#define int ll //#define endl "\n" typedef vector vi; typedef vector vvi; typedef pair pii; #define all(c) (c).begin(), (c).end() #define loop(i,a,b) for(ll i=a; i ostream & operator<<(ostream & os, vector const &); template typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple const &){} template typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple const & t){ os << (n==0?"":" ") << get(t); _ot(os, t); } template ostream & operator<<(ostream & os, tuple const & t){ _ot<0>(os, t); return os; } template ostream & operator<<(ostream & os, pair const & p){ return os << "(" << p.first << ", " << p.second << ") "; } template ostream & operator<<(ostream & os, vector const & v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; } template inline bool chmax(T & x, T const & y){ return x inline bool chmin(T & x, T const & y){ return x>y ? x=y,true : false; } #ifdef DEBUG #define dump(...) (cerr<<#__VA_ARGS__<<" = "< Edges; typedef vector Graph; typedef vector Array; typedef vector Matrix; int const inf = 1e8; // あり本のダイクストラ法 vector dijkstra(Graph const & g, int s, int t){ swap(s,t); typedef tuple State; priority_queue q; int n = g.size(); vector dist(n, inf); vector prev(n,-1); dist[s] = 0; q.emplace(0,s); while (q.size()) { Weight d; int v; tie(d,v) = q.top(); q.pop(); d *= -1; if (dist[v] < d) continue; for(auto & e : g[v]){ if (dist[e.dst] > dist[v] + e.weight) { dist[e.dst] = dist[v] + e.weight; q.emplace(-dist[e.dst], e.dst); } } } int cur = t; vector path {t}; while(cur != s){ int nx_v = inf; for(auto & e : g[cur]){ if(dist[cur] == dist[e.dst] + e.weight){ nx_v = min(nx_v, e.dst); } } cur = nx_v; path.pb(cur); } return path; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int N,M,S,G; cin >> N >> M >> S >> G; Graph g(N); rep(i,M){ int a,b,c; cin >> a >> b >> c; g[a].eb(a,b,c); g[b].eb(b,a,c); } vi path = dijkstra(g,S,G); rep(i,path.size()){ cout << path[i] << (i+1==(int)path.size() ? '\n' : ' '); } }