#include #include #include const int INF = 1000111000; int bfs(const std::vector>> &graph, int from, int to, int weight) { std::vector distances(graph.size(), INF); std::queue queue; distances[from] = 0; queue.push(from); while (!queue.empty()) { int v = queue.front(); queue.pop(); for (auto &p : graph[v]) { if (p.second >= weight && distances[p.first] == INF) { distances[p.first] = distances[v] + 1; queue.push(p.first); } } } return distances[to]; } int main() { int n, m; std::cin >> n >> m; std::vector>> graph(n, std::vector>()); for (size_t i = 0; i < m; i++) { int s, t, d; std::cin >> s >> t >> d; s--; t--; graph[s].push_back(std::make_pair(t, d)); graph[t].push_back(std::make_pair(s, d)); } int ok = 1; int ng = 1000000001; while (std::abs(ok - ng) > 1) { int mid = (ok + ng) / 2; int dist = bfs(graph, 0, n - 1, mid); if (dist < INF) { ok = mid; } else { ng = mid; } } std::cout << ok << ' ' << bfs(graph, 0, n - 1, ok) << std::endl; }