#include using namespace std; struct Dist { int cost; vector path; bool operator<(const Dist& other) const { if(cost != other.cost){ return cost < other.cost; } return path < other.path; } bool operator>(const Dist& other) const { return (other < *this); } Dist operator+(const Dist& other) const { Dist res = *this; res.cost += other.cost; res.path.insert(res.path.end(), other.path.begin(), other.path.end()); return res; } }; typedef vector> Graph; Dist dijkstra(const Graph &g, int S, int G){ const int V = g.size(); vector dist(V, {1 << 25}); dist[S] = {0, {S}}; priority_queue, greater> Q; Q.push({0, {S}}); while(!Q.empty()){ auto d = Q.top(); Q.pop(); auto pos = d.path.back(); if(dist[pos] < d){ continue; } for(const auto& e : g[pos]){ auto nd = d + e; auto npos = e.path.back(); if(nd < dist[npos]){ dist[npos] = nd; Q.push(nd); } } } return dist[G]; } int main(){ int N, M, S, G; cin >> N >> M >> S >> G; vector> g(N); for(int i=0;i> a >> b >> c; g[a].push_back(Dist{c, {b}}); g[b].push_back(Dist{c, {a}}); } auto res = dijkstra(g, S, G); for(int v : res.path){ cout << v << " "; } return 0; }