#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } constexpr int MAX_N = 1010; constexpr int MAX_M = 2510; struct Edge { int a, b, c; }; int N, M; Edge E[MAX_M]; vector G[MAX_N]; int S[MAX_M]; int dist[MAX_N]; int que[10'000'010], *qb, *qe; int on[MAX_N]; void shortest(int src, int c0) { if (chmin(dist[src], c0)) { qb = qe = que; *qe++ = src; on[src] = true; for (; qb != qe; ) { const int u = *qb++; on[u] = false; for (const int i : G[u]) { const int v = E[i].b; const int cc = max(dist[u] + S[i], -(M + 1)); if (chmin(dist[v], cc)) { if (!on[v]) { *qe++ = v; on[v] = true; } } } } } } int ans[MAX_N]; int main() { for (; ~scanf("%d%d", &N, &M); ) { for (int i = 0; i < M; ++i) { scanf("%d%d%d", &E[i].a, &E[i].b, &E[i].c); --E[i].a; --E[i].b; } sort(E, E + N, [&](const Edge &e0, const Edge &e1) { return (e0.c < e1.c); }); fill(G, G + N, vector{}); for (int i = 0; i < M; ++i) { G[E[i].a].push_back(i); } fill(S, S + M, +1); fill(dist, dist + N, M + 1); shortest(0, 0); fill(ans, ans + N, -1); for (int i = 0; i < M; ++i) { S[i] = -1; shortest(E[i].b, dist[E[i].a] - 1); // cerr<<"dist = ";pv(dist,dist+N); for (int u = 1; u < N; ++u) { if (dist[u] <= 0) { if (ans[u] == -1) { ans[u] = E[i].c; } } } } for (int u = 1; u < N; ++u) { printf("%d\n", ans[u]); } cerr<<"========"<