#include "bits/stdc++.h" using namespace std; using P = pair; struct edge {int to; long c;}; void solve() { int n, m; cin >> n >> m; vector> dist(2, vector(n, 1e18)); dist[0][0] = dist[1][0] = 0; vector g[n]; while(m--) { int a, b; long c; cin >> a >> b >> c; a--, b--; g[a].push_back({b, c}); g[b].push_back({a, c}); } priority_queue, greater

> q, q2; q.push({0, 0}); q2.push({0, 0}); while(!q.empty() || !q2.empty()) { while (!q.empty()) { auto p = q.top(); q.pop(); int v = p.second; long c = p.first; if (dist[0][v] < c) continue; for (auto np : g[v]) { int nv = np.to; long nc = np.c + dist[0][v]; if (dist[0][nv] > nc) { dist[0][nv] = nc; q.push({nc, nv}); } if (dist[1][nv] > dist[0][v]) { dist[1][nv] = dist[0][v]; q2.push({dist[0][v], nv}); } } } while(!q2.empty()) { auto p = q2.top(); q2.pop(); int v = p.second; long c = p.first; if (dist[1][v] < c) continue; for (auto np : g[v]) { int nv = np.to; long nc = np.c + dist[1][v]; if (dist[1][nv] > nc) { dist[1][nv] = nc; q2.push({nc, nv}); } } } } for (int i = 0; i < n; i++) { long ans = dist[0][i] + dist[1][i]; cout << ans << endl; } } int main(void) { solve(); //cout << "yui(*-v・)yui" << endl; return 0; }