#include #include using namespace atcoder; //#pragma GCC optimize("O3") 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; using tp = tuple; const ll INF = (1LL<<61); 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 parent, tree_size; public: union_find(unsigned long _n) : parent(_n), tree_size(_n, 1){ for(unsigned long i = 0; i < _n; ++i)parent[i] = i; } ll find(ll x){ while(parent[x] != x)x = parent[x] = parent[parent[x]]; return x; } void merge(ll a, ll b){ a = find(a); b = find(b); if(a==b) return; if(tree_size[a] < tree_size[b])swap(a, b); tree_size[a] += tree_size[b]; parent[b] = a; return; } bool same(ll a, ll b){ a = find(a); b = find(b); return a == b; } }; //uf.mergeのように使う //ufはMAX_N+1でとるか、代入時に0-indexedにする int solve(mat &G){ vec dist(N, INF); queue

que; que.emplace(0, 0); while(!que.empty()){ auto [d, v] = que.front(); que.pop(); for(int to : G[v]) if(chmin(dist[to], d + 1)) que.emplace(d + 1, to); } return dist[N - 1]; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cin>>N>>M; mat G(N); vector es; rep(i, M){ int s, t, d; cin>>s>>t>>d; --s; --t; es.emplace_back(s, t, d); } sort(ALL(es), [](auto a, auto b){ return get<2>(a) > get<2>(b); }); union_find uf(N); int w = -1; for(auto [s, t, d] : es){ if(d < w) break; G[s].push_back(t); G[t].push_back(s); uf.merge(s, t); if(w == -1 && uf.same(0, N - 1)) w = d; } cout<