#include #include using namespace std; using namespace atcoder; // デバッグ表示 #define dump(x) cout << #x << ":" << (x) << endl; // 型定義 typedef long long ll; typedef pair P; // forループ #define REP(i,n) for(ll i=0; i<(ll)(n); ++i) // 定数宣言 const int INF = 1e9; const int MOD = 1e9+7; const ll LINF = 1e18; // modint using mint = modint1000000007; // using mint = modint998244353; // グラフ表現 using Graph = vector>; // グラフの辺表現 using Edge = map,int>; // n次元配列の初期化。第2引数の型のサイズごとに初期化していく。 template void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } // コンビネーションを計算する関数 ll pow(ll N, ll k) { ll res = 1; for (ll i = 0; i < k; ++i) res *= N; return res; } // 最大公約数 ll gcd(ll a,ll b){ if (a%b == 0) return(b); else return(gcd(b, a%b)); } // 最小公倍数 ll lcm(ll a, ll b){ return a/gcd(a, b) * b; } ll N, M; vector S; vector T; vector D; bool check(ll u){ dsu A(N); REP(i, M){ ll s = S[i]; ll t = T[i]; ll d = D[i]; s--; t--; if(u <= d) A.merge(s, t); } if(A.same(0, N-1)) return true; else return false; } int main() { cout << fixed << setprecision(15); cin >> N >> M; S.resize(M); T.resize(M); D.resize(M); REP(i, M){ cin >> S[i] >> T[i] >> D[i]; } ll left = 0; ll right = LINF; while(right-left > 1) { // ループ継続条件 ll middle = ((right - left) / 2) + left; //大丈夫なときはもう少し大きい値を見る if(check(middle)){ // 範囲右寄せ,左寄せ条件 left = middle; // 左寄せ手法 } else { right = middle; // 右寄せ手法 } } // leftが最大の体重 // dump(left); Graph G(N); REP(i, M){ ll s = S[i]; ll t = T[i]; ll d = D[i]; s--; t--; if(left <= d){ G[s].push_back(t); G[t].push_back(s); } } vector dist(N, -1); queue que; dist[0] = 0; que.push(0); while(!que.empty()){ int p; p = que.front(); que.pop(); for(int next :G[p]){ // もし次のノードが訪問済みなら処理を飛ばす if(dist[next] != -1) continue; dist[next] = dist[p] + 1; que.push(next); } } // REP(i, N){ // cout << "i:" << i << " dist[i]:" << dist[i] << endl; // } cout << left << " " << dist[N-1] << endl; return 0; }