#include using namespace std; using ll = long long; struct State { int at; ll cost; ll maxc; int prev; State(int at, ll cost, int prev) : at(at), cost(cost), maxc(0), prev(prev) {} State(int at, ll cost, ll maxc, int prev) : at(at), cost(cost), maxc(maxc), prev(prev) {} bool operator>(const State& s) const { if (cost != s.cost) return cost > s.cost; if (cost + maxc != s.cost + s.maxc) return cost + maxc > s.cost + s.maxc; return maxc > s.maxc; } }; struct Edge { int to; ll cost; Edge(int to, ll cost) : to(to), cost(cost) {} }; typedef vector > AdjList; //隣接リスト const ll INF = 1e18; const int NONE = -1; AdjList graph; //sは始点、mincは最短経路のコスト、Prevは最短経路をたどる際の前の頂点 void dijkstra(int s, vector& minc, vector& Prev){ priority_queue, greater > pq; pq.push(State(s, 0, NONE)); while(!pq.empty()) { State cur = pq.top(); pq.pop(); if (minc[cur.at] <= cur.cost) continue; minc[cur.at] = cur.cost; Prev[cur.at] = cur.prev; for(Edge e : graph[cur.at]) { ll cost = cur.cost + e.cost; if (minc[e.to] <= cost) continue; pq.push(State(e.to, cost, cur.at)); } } } void dijkstra2(int s, vector& minc, vector& Prev){ priority_queue, greater > pq; vector mxc(minc.size(), 0); pq.push(State(s, 0, 0, NONE)); while(!pq.empty()) { State cur = pq.top(); pq.pop(); if (minc[cur.at] < cur.cost) continue; minc[cur.at] = cur.cost; Prev[cur.at] = cur.prev; mxc[cur.at] = cur.maxc; for(Edge e : graph[cur.at]) { ll maxc = max(e.cost, cur.maxc); ll cost = cur.cost; if (cur.maxc < maxc) { cost += cur.maxc; } else { cost += e.cost; } if (minc[e.to] < cost) continue; pq.push(State(e.to, cost, maxc, cur.at)); } } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; graph.resize(n); for (int i = 0; i < m; i++) { int a, b; ll c; cin >> a >> b >> c; a--; b--; graph[a].emplace_back(b, c); graph[b].emplace_back(a, c); } vector minc(n, INF), minc2(n, INF); vector p1(n, NONE), p2(n, NONE); dijkstra(0, minc, p1); dijkstra2(0, minc2, p2); for (int i = 0; i < n; i++) { cout << minc[i] + minc2[i] << endl; } return 0; }