#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair P; using Edge = pair; const ll INF = 1LL<<50; using Graph = vector>; template vector dijkstra1(const Graph &G, int s){ vector dist((int)G.size(),INF); priority_queue, greater> que; dist[s] = 0; que.push(Edge(0,s));//頂点と暫定距離 while (!que.empty()){ Edge e = que.top(); que.pop(); int v = e.second; if (dist[v] < e.first) continue; for (auto p : G[v]) { if (dist[p.first] > dist[v] + p.second) { dist[p.first] = dist[v] + p.second; que.push(make_pair(dist[p.first],p.first)); } } } return dist; } template vector> dijkstra2(const Graph &G, int s){ vector> dist(2,vector((int)G.size(),INF)); priority_queue, vector>, greater> > que; dist[0][s] = 0; que.push(make_pair(Edge(0,s),0));//チケットを使ったかと頂点 while (!que.empty()){ pair e = que.top(); que.pop(); int v = e.first.second; int use = e.second; if (dist[use][v] < e.first.first) continue; for (auto p : G[v]) { if (dist[use][p.first] > dist[use][v] + p.second) { dist[use][p.first] = dist[use][v] + p.second; que.push(make_pair(Edge(dist[use][p.first],p.first),use)); } if (!use) { if (dist[1][p.first] < e.first.first) continue; dist[1][p.first] = e.first.first; que.push(make_pair(Edge(dist[1][p.first],p.first),1)); } } } return dist; } int main(){ int n, m; cin >> n >> m; Graph G(n); rep(i,m) { int a, b, c; cin >> a >> b >> c; a--;b--; G[a].push_back({b,c}); G[b].push_back({a,c}); } auto dist1 = dijkstra1(G,0); auto dist2 = dijkstra2(G,0); rep(i,n) { cout << dist1[i] + min(dist2[0][i], dist2[1][i]) << endl; } return 0; }