#define PROBLEM "https://yukicoder.me/problems/no/1473" #include #include #include #include #include #include template class Graph { //using Node = int; //using Cost = long long; using Edge = std::pair; using Edges = std::vector; const int m_n; std::vector m_graph; public: Graph(int n) :m_n(n), m_graph(n) {} auto addEdge(const Node& f, const Node& t, const Cost& c = 1) { m_graph[f].emplace_back(t, c); } auto addEdgeUndirected(const Node& f, const Node& t, const Cost& c = 1) { addEdge(f, t, c); addEdge(t, f, c); } auto getEdges(const Node& from)const { class EdgesRange { const typename Edges::const_iterator b, e; public: EdgesRange(const Edges& edges) :b(edges.begin()), e(edges.end()) {} auto begin()const { return b; } auto end()const { return e; } }; return EdgesRange(m_graph[from]); } auto getEdges()const { std::deque> edges; for(Node from = 0; from < m_n; ++from) for(const auto& [to, c] : getEdges(from)) { edges.emplace_back(from, to, c); } return edges; } auto getEdgesExcludeCost()const { std::deque> edges; for(Node from = 0; from < m_n; ++from) for(const auto& [to, _] : getEdges(from)) { edges.emplace_back(from, to); } return edges; } auto reverse()const { auto rev = Graph(m_n); for(const auto& [from, to, c] : getEdges()) { rev.addEdge(to, from, c); } return rev; } auto size()const { return m_n; }; auto debug(bool directed = false)const { for(const auto& [f, t, c] : getEdges())if(f < t || directed) { std::cout << f << " -> " << t << ": " << c << std::endl; } } }; template auto binarySearch(long long ok, long long ng, const Lambda& is_ok) { while(std::abs(ok - ng) > 1) { long long mid = (ok + ng) >> 1; (is_ok(mid) ? ok : ng) = mid; } return ok; } template auto bfs(const Graph& graph, const Node& root, const Lambda& lambda) { auto n = graph.size(); std::vector used(n); used[root] = true; std::queue q; q.emplace(root); while(!q.empty()) { auto from = q.front(); q.pop(); for(const auto& [to, _] : graph.getEdges(from)) { if(used[to]) { continue; } q.emplace(to); used[to] = true; lambda(from, to); } } } using ll = long long; using std::cout; using std::cin; constexpr char endl = '\n'; signed main() { ll n, m; cin >> n >> m; auto graph_all = Graph(n); for(int i = 0; i < m; ++i) { ll s, t, d; cin >> s >> t >> d; graph_all.addEdgeUndirected(s - 1, t - 1, d); } auto solve = [&](ll w) { auto graph = Graph(n); for(const auto& [s, t, d] : graph_all.getEdges()) { if(w <= d) { graph.addEdge(s, t); } } std::vector dv(n); bfs(graph, 0, [&](ll f, ll t) {dv[t] = dv[f] + 1; }); return dv[n - 1]; }; auto w_max = binarySearch(0, 1e9, [&](ll w) { auto d = solve(w); return d > 0; }); auto ans = solve(w_max); cout << w_max << " " << ans << endl; }