#include #include #include #include #include #include #include using namespace std; const int INF = 1e7; vector> dist_matrix; vector> connect; vector distances; void dijkstra(int start, int goal) { distances.assign(dist_matrix.size(), INF); distances[start] = 0; priority_queue> que; que.push(make_pair(0, start)); while (!que.empty()) { auto p = que.top(); int now_dist = p.first; int from = p.second; que.pop(); for (auto to : connect[from]) { int d = now_dist + dist_matrix[from][to]; if (d < distances[to]) { distances[to] = d; que.push(make_pair(d, to)); } } } } bool dfs(list &route, int goal) { int now = route.back(); for (auto to : connect[now]) { if (distances[now] + dist_matrix[now][to] == distances[to]) { route.emplace_back(to); if (to == goal) { return true; } if (dfs(route, goal)) { return true; } route.pop_back(); } } return false; } int main() { int n, m, s, g, a, b, c; cin >> n >> m >> s >> g; dist_matrix.assign(n, vector(n, INF)); connect.assign(n, vector()); for (int i = 0; i < m; i++) { cin >> a >> b >> c; dist_matrix[a][b] = c; dist_matrix[b][a] = c; connect[a].emplace_back(b); connect[b].emplace_back(a); } for (auto &v : connect) { sort(v.begin(), v.end()); } dijkstra(s, g); list route = {s}; dfs(route, g); for (auto x : route) { cout << x; if (x != g) { cout << " "; } else { cout << endl; } } return 0; }