//g++ 4.cpp -std=c++14 -O2 -I . #include using namespace std; #include using namespace atcoder; using ll = long long; using ld = long double; using vi = vector; using vvi = vector; using vll = vector; using vvll = vector; using vld = vector; using vvld = vector; #define fi first #define se second #define pb push_back #define all(a) a.begin(),a.end() #define rep(i,start,end) for(ll i=start;i<(ll)(end);i++) #define per(i,start,end) for(ll i=start;i>=(ll)(end);i--) using T = tuple; using P = pair; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,m; cin>>n>>m; vector g; rep(i,0,m){ int s,t; ll d; cin>>s>>t>>d; s--;t--; g.pb({s,t,d}); } ll ng=1e18,ok=0; while(ng-ok>1){ ll check=(ng+ok)/2; dsu d(n); rep(i,0,m){ auto[a,b,c]=g[i]; if(c>=check){ d.merge(a,b); } } if(d.same(0,n-1)==true){ ok=check; } else{ ng=check; } } vvi graph(n); rep(i,0,m){ auto[a,b,c]=g[i]; if(c>=ok){ graph[a].pb(b); graph[b].pb(a); } } priority_queue,greater

> que; vi dist(n,1e6); que.push({0,0}); dist[0]=0; while(!que.empty()){ auto[now,time]=que.top(); que.pop(); if(time>dist[now])continue; for(int next:graph[now]){ if(dist[next]>dist[now]+1){ dist[next]=dist[now]+1; que.push({next,dist[now]+1}); } } } cout<