// Here is a C++17 solution to this problem. using namespace std; #define f first #define s second #define pb push_back #define mp make_pair typedef long long ll; typedef pair<int,int> pi; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int main() { int n, m; cin >> n >> m; vector<vector<pi>> adj(n); for (int i=0; i<m; i++) { int u, v, w; cin >> u >> v >> w; u--;v--; adj[v].pb(mp(u, w)); adj[u].pb(mp(v, w)); } vll dist(n, -1); vi prev(n, -1); vector<bool> vis(n); priority_queue<pll, vector<pll>, greater<pll>> Q; Q.push(mp(0, 0)); dist[0] = 0; vis[0] = true; while(!Q.empty()) { auto a = Q.top(); Q.pop(); vis[a.s] = true; for(auto &p : adj[a.s]) { if(!vis[p.f]) { Q.push(mp(a.f + p.s, p.f)); if(dist[p.f] == -1 || dist[p.f] > dist[a.s] + p.s) { dist[p.f] = dist[a.s] + p.s; prev[p.f] = a.s; } } } } if(dist[n-1] == -1) cout << -1 << endl; else { vi ans; int cur = n-1; while(cur != -1) { ans.pb(cur); cur = prev[cur]; } reverse(ans.begin(), ans.end()); for(auto x : ans) cout << x+1 << " "; cout << endl; } }