#include using namespace std; using ll = long long; #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 2; template struct edge{ int from; int to; T cost; int id; edge(){} edge(int to, T cost=1) : from(-1), to(to), cost(cost){} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){} void reverse(){swap(from, to);} }; template struct edges : std::vector>{ void sort(){ std::sort( (*this).begin(), (*this).end(), [](const edge& a, const edge& b){ return a.cost < b.cost; } ); } }; template struct graph : std::vector>{ private: int n = 0; int m = 0; edges es; bool dir; public: graph(int n, bool dir) : n(n), dir(dir){ (*this).resize(n); } void add_edge(int from, int to, T cost=1){ if(dir){ es.push_back(edge(from, to, cost, m)); (*this)[from].push_back(edge(from, to, cost, m++)); }else{ if(from > to) swap(from, to); es.push_back(edge(from, to, cost, m)); (*this)[from].push_back(edge(from, to, cost, m)); (*this)[to].push_back(edge(to, from, cost, m++)); } } int get_vnum(){ return n; } int get_enum(){ return m; } bool get_dir(){ return dir; } edge get_edge(int i){ return es[i]; } edges get_edge_set(){ return es; } }; template struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template using Edges = vector>; template using weighted_graph = vector>; template using tree = vector>; using unweighted_graph = vector>; template using residual_graph = vector>>; class shortest_path{ private: // restoreable dijkstra template struct rdijkstra{ private: const T TINF = numeric_limits::max()/3; int n, s; graph G; vector dist; vector vpar; edges epar; public: rdijkstra(graph G, int s) : G(G), s(s){ // initilization n = G.get_vnum(); dist.resize(n, TINF); vpar.resize(n, -1); epar.resize(n, -1); // running Dijkstra algorithm run(); } void run(){ dist[s] = 0; priority_queue, vector>, greater<>> que; que.push({0, s}); while(!que.empty()){ auto [d, v] = que.top(); que.pop(); if(dist[v] < d) continue; for(auto e : G[v]){ if(dist[v] + e.cost < dist[e.to]){ dist[e.to] = dist[v] + e.cost; vpar[e.to] = v; epar[e.to] = e; que.push({dist[e.to], e.to}); } } } } T get_dist(int t){ return dist[t]; } vector get_dist(){ return dist; } vector get_vpar(){ return vpar; } int get_vpar(int v){ return vpar[v]; } edges get_epar(){ return epar; } edge get_epar(int v){ return epar[v]; } vector get_vpath(int t){ vector vpath; int cur = t; while(cur != -1){ vpath.push_back(cur); cur = vpar[cur]; } reverse(vpath.begin(), vpath.end()); return vpath; } edges get_epath(int t){ edges epath; int cur = t; while(cur != s){ epath.push_back(epar[cur]); cur = vpar[cur]; } reverse(epath.begin(), epath.end()); return epath; } graph get_shotest_path_tree(){ graph spt(n, false); for(int v=0; v static vector bfs(graph &G, int s){ int n = G.get_vnum(); vector dist(n, -1); dist[s] = 0; queue que; que.push(s); while(!que.empty()){ int v = que.front(); que.pop(); for(auto e : G[v]) if(dist[e.to]==-1){ dist[e.to] = dist[v] + 1; que.push(e.to); } } return dist; } template static vector binary_bfs(graph &G, int s){ int n = G.get_vnum(); vector dist(n, -1); dist[s] = 0; deque deq; deq.push_front(s); while(!deq.empty()){ int v = deq.front(); deq.pop_front(); for(auto e : G[v]) if(dist[e.to]==-1){ dist[e.to] = dist[v] + e.cost; if(e.cost) deq.push_back(e.to); else deq.push_front(e.to); } } return dist; } template static vector constant_bfs(graph &G, int s, T W){ int n = G.get_vnum(); vector dist(n, -1); vector> cand(n*W+1); dist[s] = 0; cand[0].push_back(s); for(int d=0; d<=n*W; d++) for(int v : cand[d]){ if(dist[v]!=-1) continue; for(auto e : G[v]) if(dist[v] + dist[e.to] < dist[e.ot]){ dist[e.to] = dist[v] + e.cost; cand[dist[e.to]].push_back(e.to); } } return dist; } template static vector complement_bfs(graph &G, int s){ int n = G.get_vnum(); map, bool> mp; for(int v=0; v dist(n, -1); vector unvisited; for(int v=0; v visited; visited.push(s); dist[s] = 0; while(!visited.empty()){ int v = visited.front(); visited.pop(); vector nxt; for(int to : unvisited){ if(!mp[{v, to}]){ visited.push(to); dist[to] = dist[v]+1; }else{ nxt.pb(to); } } unvisited = nxt; } return dist; } template static vector bellman_ford(graph &G, int s){ int n = G.get_vnum(); bool dir = G.get_dir(); const T TINF = numeric_limits::max()/3; edges es = G.get_edge_set(); vector dist(n, TINF); vector flag(n, false); dist[s] = 0; for(int i=0; i static vector> warshall_floyd(graph &G){ int n = G.get_vnum(); const T TINF = numeric_limits::max()/3; vector> dist(n, vector(n, TINF)); for(int v=0; v static vector>> pered(graph &G, int k){ int n = G.get_vnum(); const T TINF = numeric_limits::max()/3; priority_queue, vector>, greater<>> que; vector>> neibors(n); vector> mp(n); for(int v=0; v static vector malick_mittal_gupta(graph &G, int s, int t){ // declear variable const T TINF = numeric_limits::max()/3; rdijkstra dijk_s(G, s), dijk_t(G, t); int n = G.get_vnum(); int m = G.get_enum(); vector dist_s = dijk_s.get_dist(); vector dist_t = dijk_t.get_dist(); vector path = dijk_s.get_vpath(t); int p = (int)path.size(); path.push_back(n); vector> ch(n); for(int v=0; v label(n, -1); function labeling = [&](int v, int l){ label[v] = l; for(int to : ch[v]) labeling(to, l); }; for(int i=0; i used(m, false); for(int i=1; i> sevt(p), eevt(p); for(int v=0; v ans(m, dijk_s.get_dist(t)); set> eset; for(int i=1; i label[y]) swap(x, y); eset.insert({dist_s[x]+e.cost+dist_t[y], id}); } // calc ans if(!eset.empty()) ans[f.id] = min(ans[f.id], (*eset.begin()).first); // end event with label = i for(int id : eevt[i]){ auto e = G.get_edge(id); int x = e.from, y = e.to; if(label[x] > label[y]) swap(x, y); eset.erase({dist_s[x]+e.cost+dist_t[y], id}); } } return ans; } template static vector roditty_zwick(graph &G, int s, int t){ int n = G.get_vnum(); int m = G.get_enum(); const T TINF = numeric_limits::max()/2; int log_n = 0, sqrt_n = 0; int sn = n; while(sn) sn/=2, log_n++; while(sqrt_n*sqrt_n vpar(n, -1), epar(n, -1), sdist(n, IINF); auto bfs1 = [&](int s){ queue que; que.push(s); sdist[s] = 0; while(!que.empty()){ int v = que.front(); que.pop(); for(auto e : G[v]) if(sdist[e.to]==IINF){ sdist[e.to] = sdist[v] + 1; vpar[e.to] = v; epar[e.to] = e.id; que.push(e.to); } } }; bfs1(s); vector vpath, epath; vector ans(m, sdist[n-1]); if(sdist[n-1]==IINF) return ans; int now = t; while(now != -1){ vpath.push_back(now); if(now != 0){ epath.push_back(epar[now]); ans[epar[now]] = IINF; } now = vpar[now]; } reverse(vpath.begin(), vpath.end()); reverse(epath.begin(), epath.end()); int p = (int)vpath.size(); graph H(n, true), RH(n, true); for(int v=0; v dist(n, -1); vector> vec(2*n); for(int j=i; j &g, int r, vector &dist){ queue que; que.push(r); dist[r] = 0; while(!que.empty()){ int v = que.front(); que.pop(); for(auto e : g[v]) if(dist[e.to]==-1){ dist[e.to] = dist[v] + 1; que.push(e.to); } } }; // find long detours (randomized) vector check_path_vertex(n, false); for(int i=0; i rest; for(int v=0; v dist(n, -1), rdist(n, -1); bfs2(H, r, dist); bfs2(RH, r, rdist); vector rmin(p+1, IINF); for(int i=p-1; i>=0; i--){ rmin[i] = rmin[i+1]; if(dist[vpath[i]]!=-1){ rmin[i] = min(rmin[i+1], dist[vpath[i]]+(p-1-i)); } } int mn = IINF; for(int i=0; i static vector yen(graph &G, int s, int t, int k){ } template static vector dijkstra(graph &G, int s){ int n = G.get_vnum(); const T TINF = numeric_limits::max()/3; vector dist(n, TINF); dist[s] = 0; priority_queue, vector>, greater<>> que; que.push({0, s}); while(!que.empty()){ auto [d, v] = que.top(); que.pop(); if(dist[v] < d) continue; for(auto e : G[v]){ if(dist[v] + e.cost < dist[e.to]){ dist[e.to] = dist[v] + e.cost; que.push({dist[e.to], e.to}); } } } return dist; } }; void solve(){ int n, m, s, t; cin >> n >> m >> s >> t; graph G(n, false); for(int i=0; i> u >> v; ll w; cin >> w; G.add_edge(u, v, w); } auto dist = shortest_path::dijkstra(G, s); vector par(n, -1); vector visited(n, false); function dfs = [&](int v){ visited[v] = true; vector> nxt; for(auto e : G[v]) nxt.pb({e.to, e.cost}); sort(all(nxt)); for(auto [nxt, cost] : nxt){ if(dist[nxt] == dist[v] + cost){ if(!visited[nxt]){ par[nxt] = v; dfs(nxt); } } } }; dfs(s); vector ans; while(t != -1){ ans.pb(t); t = par[t]; } reverse(all(ans)); for(int v : ans) cout << v << ' '; cout << '\n'; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }