#include #define rep(i, a, n) for(int i = a; i < n; i++) #define int long long using namespace std; typedef pair P; const int mod = 1000000007; const int INF = 1e18; int n, m; const int MAX_V = 100010; struct edge{ int to, cost; edge(int to, int cost):to(to), cost(cost){} }; struct S{ int cost, to; bool flag; S(int cost, int to, bool flag):cost(cost), to(to), flag(flag){} bool operator>(const S &s) const{ return cost > s.cost; } }; vector G[MAX_V]; struct Dijkstra{ vector d, e; Dijkstra(){} Dijkstra(int V){ d.resize(V, INF); e.resize(V, INF); } void calc(int s){ d[s] = 0; e[s] = 0; priority_queue, greater > q; q.push({d[s], s, false}); while(!q.empty()){ S p = q.top(); q.pop(); int from = p.to; int cost = p.cost; bool f = p.flag; if(d[from] < cost) continue; rep(i, 0, G[from].size()){ int next = G[from][i].to; int newCost = cost + G[from][i].cost; if(f == false){ if(d[next] > newCost){ d[next] = newCost; q.push({newCost, next, f}); } if(e[next] > cost){ e[next] = cost; q.push({cost, next, true}); } }else{ if(e[next] > newCost){ e[next] = newCost; q.push({newCost, next, f}); } } } } } }; signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cin >> n >> m; rep(i, 0, m){ int u, v, c; cin >> u >> v >> c; u--; v--; G[u].push_back({v, c}); G[v].push_back({u, c}); } Dijkstra ds(n); ds.calc(0); rep(i, 0, n){ int ans = ds.d[i] + ds.e[i]; cout << ans << endl; } }