#define _USE_MATH_DEFIMES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include const int MOD = 1'000'000'007; const int MOD2 = 998'244'353; const int INF = 1'000'000'000; //1e9 const int NIL = -1; const long long LINF = 1'000'000'000'000'000'000; // 1e18 const long double EPS = 1E-10L; template inline bool chmax(T &a, const S &b){ if(a < b){a = b; return true;} return false; } template inline bool chmin(T &a, const S &b){ if(b < a){a = b; return true;} return false; } template inline bool exist(Container &s, const T &e){ return (s.find(e) != std::end(s)); } template inline bool inside(T x, T lx, T rx){ return (std::clamp(x, lx, rx) == x); } template inline bool inside(T x, T y, T lx, T rx, T ly, T ry){ return inside(x, lx, rx) && inside(y, ly, ry); } struct edge{ int to, cost; edge(int To, int Cost): to(To), cost(Cost){} }; void dijkstra(int s, std::vector> &G, std::vector &d, std::vector &prv){ //pair first: 距離 second: 頂点 std::priority_queue, std::vector>, std::greater>> que; int V{int(G.size())}; d.resize(V, LINF+3); prv.resize(V, NIL); d[s] = 0; que.emplace(0, s); while(!que.empty()){ std::pair p{que.top()}; que.pop(); int v{p.second}; if(d[v] < p.first) continue; for(edge &e: G[v]){ if(d[e.to] > d[v] + e.cost){ d[e.to] = d[v] + e.cost; prv[e.to] = v; que.emplace(d[e.to], e.to); } } } } int main(){ int N, M, S, G; std::cin >> N >> M >> S >> G; std::vector> graph(N); { int a, b, c; for(int i{0}; i < M; ++i){ std::cin >> a >> b >> c; graph[a].emplace_back(b, c); graph[b].emplace_back(a, c); } } /*for(int i{0}; i < N; ++i){ std::sort(std::begin(graph[i]), std::end(graph[i]), [](edge &a, edge &b){return a.to < b.to;}); }*/ std::vector prv; std::vector d; dijkstra(G, graph, d, prv); std::vector ans; ans.reserve(N); ans.push_back(S); int cur{S}; while(cur != G){ int dist{int(d[cur])}, v{N}; for(auto [to, cost]: graph[cur]){ if(d[to] + cost == dist) chmin(v, to); } cur = v; ans.push_back(v); } for(auto itr{std::begin(ans)}; itr != std::end(ans); ++itr){ if(itr != std::begin(ans)) std::cout << " "; std::cout << *itr; } std::cout << std::endl; return 0; }