#include #include using namespace std; template struct Edge { int to; T cost; }; template struct WeightedGraph { int n; std::vector>> g; WeightedGraph(){} WeightedGraph(int n) : n(n){ g.resize(n); } void add_edge(int from, int to, T cost){ g[from].push_back((Edge){to, cost}); } }; int dist(WeightedGraph &g, int k){ int n = g.n; int d[100005]; for(int i = 0; i < n; i++) d[i] = -1; queue que; d[0] = 0; que.push(0); while(que.size()){ int u = que.front(); que.pop(); for(Edge e : g.g[u]){ if(e.cost < k) continue; int v = e.to; if(d[v] == -1){ d[v] = d[u] + 1; que.push(v); } } } return d[n - 1]; } int main() { int n, m; cin >> n >> m; WeightedGraph g(n); for(int i = 0; i < m; i++){ int s, t, d; cin >> s >> t >> d; s--; t--; g.add_edge(s, t, d); g.add_edge(t, s, d); } int left = 0, right = 1000000009; while(right - left > 1){ int mid = (right + left) / 2; if(dist(g, mid) >= 0) left = mid; else right = mid; } cout << left << " " << dist(g, left) << endl; }