// yukicoder: No.807 umg tours // 2019.7.10 bal4u #include #include #include typedef long long ll; #if 1 #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) #else #define gc() getchar() #define pc(c) putchar(c) #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } void out(long n) { // 正整数の表示(出力) int i; char b[20]; i = 0; while (n) b[i++] = n % 10 + '0', n /= 10; while (i--) pc(b[i]); pc('\n'); } #define INF 0x33 #define QMAX 1000005 #define MAX 100005 int *to[MAX], *dis[MAX], hi[MAX]; int N; ll ma[MAX], mi[MAX]; int q[QMAX], top, end; // queue void bfs() { int i, a, b, f; memset(mi, INF, sizeof(ll)*N); memset(ma, INF, sizeof(ll)*N); for (i = 0; i < hi[0]; i++) { b = to[0][i]; q[end++] = b, ma[b] = dis[0][i], mi[b] = 0; } while (top != end) { a = q[top++]; // printf("a=%d, ma=%lld, mi=%lld\n", a, ma[a], mi[a]); for (i = 0; i < hi[a]; i++) { b = to[a][i], f = 0; if (ma[b] > dis[a][i]+ma[a]) ma[b] = ma[a] + dis[a][i], f = 1; if (mi[b] > dis[a][i]+mi[a]) mi[b] = mi[a] + dis[a][i], f = 1; if (mi[b] > ma[a]) mi[b] = ma[a], f = 1; if (f) q[end++] = b; } } } int main() { int i, k, a, b, c, M; int *memo, sz; N = in(), M = in(); memo = malloc(sizeof(int)*M*3); sz = 0; while (M--) { memo[sz++] = a = in()-1, memo[sz++] = b = in()-1, memo[sz++] = in(); hi[a]++, hi[b]++; } for (i = 0; i < N; i++) if (hi[i]) { to[i] = malloc(sizeof(int)*hi[i]); dis[i] = malloc(sizeof(int)*hi[i]); } memset(hi, 0, sizeof(hi)); i = 0; while (i < sz) { a = memo[i++], b = memo[i++], c = memo[i++]; k = hi[a]++, to[a][k] = b, dis[a][k] = c; k = hi[b]++, to[b][k] = a, dis[b][k] = c; } bfs(); pc('0'), pc('\n'); for (i = 1; i < N; i++) out(ma[i] + mi[i]); return 0; }