#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/bfs.hpp" #line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/graph.hpp" #include template struct Edge { int src, dst; Cost cost; Edge() = default; Edge(int src, int dst, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge& e) const { return cost < e.cost; } bool operator>(const Edge& e) const { return cost > e.cost; } }; template struct Graph : public std::vector>> { using std::vector>>::vector; void span(bool direct, int src, int dst, Cost cost = 1) { (*this)[src].emplace_back(src, dst, cost); if (!direct) (*this)[dst].emplace_back(dst, src, cost); } }; #line 4 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/bfs.hpp" #include template std::vector bfs(const Graph& graph, int s) { std::vector dist(graph.size(), -1); dist[s] = 0; std::queue que; que.push(s); while (!que.empty()) { auto v = que.front(); que.pop(); for (const auto& e : graph[v]) { if (dist[e.dst] != -1) continue; dist[e.dst] = dist[v] + e.cost; que.push(e.dst); } } return dist; } #line 2 "main.cpp" #include #include #include #line 6 "main.cpp" #include using namespace std; void solve() { int n, m; cin >> n >> m; vector> es(m); for (auto& [d, u, v] : es) { cin >> u >> v >> d; --u, --v; } sort(es.rbegin(), es.rend()); atcoder::dsu dsu(n); Graph<> graph(n); for (auto [d, u, v] : es) { dsu.merge(u, v); graph.span(false, u, v); if (dsu.same(0, n - 1)) { cout << d << " " << bfs(graph, 0)[n - 1] << "\n"; return; } } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }