/** * @FileName a.cpp * @Author kanpurin * @Created 2020.05.24 20:00:07 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; // dijkstra O(ElogV) // verify : https://onlinejudge.u-aizu.ac.jp/problems/GRL_1_A // ※拡張ダイクストラ template struct Dijkstra { private: int V; struct edge { int to; T cost; }; public: vector> G; const T inf = numeric_limits::max(); // s から i の最小コスト // 経路がない場合は inf vector d; // (頂点) ※ Dijkstra(int V) : V(V) { G.resize(V); } // 辺の追加 // 有向の場合 directed = true void add_edge(int from, int to, T weight, bool directed = false) { G[from].push_back({to,weight}); if (!directed) G[to].push_back({from,weight}); } void build(int s) { d.assign(V, inf); // ※ typedef tuple P; //(距離,頂点) ※ priority_queue, greater

> pq; d[s] = 0; // ※ pq.push(P(d[s], s)); // ※ while (!pq.empty()) { P p = pq.top(); pq.pop(); int v = get<1>(p); // ※ if (d[v] < get<0>(p)) continue; // ※ for (const edge &e : G[v]) { // ※ if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; pq.push(P(d[e.to], e.to)); } } } } }; int main() { int n, m; cin >> n >> m; int sv, gv; cin >> sv >> gv; Dijkstra g(n); for (int i = 0; i < m; i++) { int u, v, c; cin >> u >> v >> c; g.add_edge(u,v,c); } g.build(gv); vector ans; int v = sv; ans.push_back(v); while(v != gv) { int u = n; for(auto e : g.G[v]) { if (g.d[e.to] + e.cost == g.d[v]) { u = min(u,e.to); } } v = u; ans.push_back(v); } for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << endl; return 0; }