#include //#include //#pragma GCC optimize("Ofast") using namespace std; #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() using ll = long long; using vec = vector; using mat = vector; ll N,M,H,W,Q,K,A,B; string S; using P = pair; const ll INF = (1LL<<60); template bool chmin(T &a, const T b){ if(a > b) {a = b; return true;} else return false; } template bool chmax(T &a, const T b){ if(a < b) {a = b; return true;} else return false; } template void my_printv(std::vector v,bool endline = true){ if(!v.empty()){ for(std::size_t i{}; i; void bfs(vector &G, vec &dist){ dist[0] = 0; queue que; que.push(0); while(!que.empty()){ int v = que.front(); que.pop(); for(edge &e : G[v]){ if(chmin(dist[e.to], dist[v] + 1)){ que.push(e.to); } } } } void dfs(ll v, ll id, vec &dist, vector &G, mat &res){ for(edge &e : G[v]){ ll new_cost = dist[v] + (e.ord <= id ? -1 : 1); if(new_cost <= - N) new_cost = -INF; if(dist[e.to] > new_cost){ if(dist[e.to] > 0 && new_cost <= 0) res[id].push_back(e.to); dist[e.to] = new_cost; dfs(e.to, id, dist, G, res); } } } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cin>>N>>M; vector G(N); vec u(M), v(M), w(M), ord(M), dist(N, INF); mat res(M); rep(i, M){ cin>>u[i]>>v[i]>>w[i]; --u[i]; --v[i]; } iota(ALL(ord), 0); sort(ALL(ord), [&](int x, int y){return w[x] < w[y];}); rep(_, M) { int i = ord[_]; G[u[i]].emplace_back(v[i], _); } bfs(G, dist); //my_printv(dist); rep(i, M) dfs(u[ord[i]], i, dist, G, res); vec ans(N, -1); rep(i, M) for(int j : res[i]) ans[j] = w[ord[i]]; reps(i, 1, N) cout<