#include #include template std::vector vec(int len, T elem) { return std::vector(len, elem); } template struct Edge { int src, dst; Cost cost; Edge(int src = -1, int dst = -1, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge& e) const { return this->cost < e.cost; } bool operator>(const Edge& e) const { return this->cost > e.cost; } }; template struct Graph { std::vector>> graph; Graph(int n = 0) : graph(n) {} void span(bool direct, int src, int dst, Cost cost = 1) { graph[src].emplace_back(src, dst, cost); if (!direct) graph[dst].emplace_back(dst, src, cost); } int size() const { return graph.size(); } void clear() { graph.clear(); } void resize(int n) { graph.resize(n); } std::vector>& operator[](int v) { return graph[v]; } std::vector> operator[](int v) const { return graph[v]; } }; using lint = long long; constexpr lint INF = 1LL << 30; void solve() { int n, m, s, g; std::cin >> n >> m >> s >> g; Graph graph(n); auto dist = vec(n, vec(n, INF)); for (int v = 0; v < n; ++v) dist[v][v] = 0; while (m--) { int u, v, c; std::cin >> u >> v >> c; dist[u][v] = dist[v][u] = c; graph.span(false, u, v, c); } for (int j = 0; j < n; ++j) { for (int i = 0; i < n; ++i) { for (int k = 0; k < n; ++k) { dist[i][k] = std::min(dist[i][k], dist[i][j] + dist[j][k]); } } } while (s != g) { std::cout << s << " "; auto d = dist[s][g]; int nxt = n; for (auto e : graph[s]) { int v = e.dst; if (e.cost + dist[v][g] == d) { nxt = std::min(nxt, v); } } s = nxt; } std::cout << g << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }