/** * @FileName a.cpp * @Author kanpurin * @Created 2021.04.11 17:45:59 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; class UnionFind { private: vector par; public: UnionFind(int n) { par.resize(n, -1); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } bool unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return false; if (size(rx) < size(ry)) swap(rx, ry); par[rx] += par[ry]; par[ry] = rx; return true; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } int size(int x) { return -par[root(x)]; } }; int main() { int n;cin >> n; int m;cin >> m; vector s(m),t(m),d(m),idx(m); iota(idx.begin(), idx.end(),0); for (int i = 0; i < m; i++) { cin >> s[i] >> t[i] >> d[i]; s[i]--; t[i]--; } sort(idx.begin(), idx.end(), [&](int a,int b){ return d[a] > d[b]; }); UnionFind uf(n); vector> g(n); for (int i = 0; i < m; i++) { int e = idx[i]; while(i < m && d[e] == d[idx[i]]) { uf.unite(s[idx[i]],t[idx[i]]); g[s[idx[i]]].push_back(t[idx[i]]); g[t[idx[i]]].push_back(s[idx[i]]); i++; } i--; if (!uf.same(0,n-1)) continue; cout << d[e] << " "; queue que; vector dist(n,-1); que.push(0); dist[0] = 0; while(!que.empty()) { int q = que.front(); que.pop(); for(int v : g[q]) { if (dist[v] != -1) continue; dist[v] = dist[q] + 1; que.push(v); } } cout << dist[n-1] << endl; break; } return 0; }