#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define ll long long #define ld long double #define EPS 0.0000000001 #define INF 1e9 #define LINF (ll)INF*INF #define MOD 1000000007 #define rep(i,n) for(int i=0;i<(n);i++) #define loop(i,a,n) for(int i=a;i<(n);i++) #define all(in) in.begin(),in.end() #define shosu(x) fixed< vi; typedef vector vs; typedef pair pii; typedef vector vp; typedef pair pip; int gcd(int a, int b){ if(b==0) return a; return gcd(b,a%b); } int lcm(int a, int b){ return a/gcd(a,b)*b; } class Dijkstra{ private: struct edge { int to, cost;}; vector< vector > g; public: Dijkstra(){}; Dijkstra(int size){ g.resize(size); } vector dist; void add_edge(int a, int b, int c){//無向辺 edge e1 = {b, c}, e2 = {a, c}; g[a].push_back(e1); g[b].push_back(e2);//有向辺の場合はここをコメントアウトする } void calc(int s){ priority_queue< pip, vector, greater > q; int n = g.size(); dist = vector(n, vi(2,LINF)); dist[s][0] = dist[s][1] = 0; q.push(pip(0, pii(s,0))); q.push(pip(0, pii(s,1))); while(!q.empty()){ pip p = q.top(); q.pop(); int v = p.second.first; int cnt = p.second.second; if(dist[v][cnt] < p.first)continue; //更新されていたら何もしない for(int i = 0; i < g[v].size(); i++){ edge e = g[v][i]; if(!cnt && dist[e.to][cnt+1] > dist[v][cnt]){ dist[e.to][cnt+1] = dist[v][cnt]; q.push(pip(dist[e.to][cnt+1], pii(e.to,cnt+1))); } if(dist[e.to][cnt] > dist[v][cnt] + e.cost){ dist[e.to][cnt] = dist[v][cnt] + e.cost; q.push(pip(dist[e.to][cnt], pii(e.to,cnt))); } } } } }; signed main(void) { int n,m; cin >> n >> m; Dijkstra d(n); rep(i,m){ int a,b,c; cin >> a >> b >> c; a--,b--; d.add_edge(a,b,c); } d.calc(0); rep(i,n){ cout << d.dist[i][0] + d.dist[i][1] << endl; } }