#include using i32 = std::int32_t; using u32 = std::uint32_t; using i64 = std::int64_t; using u64 = std::uint64_t; using i128 = __int128_t; using u128 = __uint128_t; using isize = std::ptrdiff_t; using usize = std::size_t; class rep { struct Iter { usize itr; constexpr Iter(const usize pos) noexcept : itr(pos) {} constexpr void operator++() noexcept { ++itr; } constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; } constexpr usize operator*() const noexcept { return itr; } }; const Iter first, last; public: explicit constexpr rep(const usize first, const usize last) noexcept : first(first), last(std::max(first, last)) {} constexpr Iter begin() const noexcept { return first; } constexpr Iter end() const noexcept { return last; } }; class UnionFind { usize components; std::vector data; public: explicit UnionFind(const usize size = 0) : components(size), data(size, (usize)-1) {} usize size() const { return data.size(); } usize count() const { return components; } usize leader(const usize u) { assert(u < size()); if (data[u] >= size()) return u; return data[u] = leader(data[u]); } usize size(const usize u) { assert(u < size()); return -data[leader(u)]; } std::pair merge(usize u, usize v) { assert(u < size()); assert(v < size()); u = leader(u); v = leader(v); if (u == v) return std::make_pair(u, false); if (data[u] > data[v]) std::swap(u, v); components -= 1; data[u] += data[v]; data[v] = u; return std::make_pair(u, true); } bool same(const usize u, const usize v) { assert(u < size()); assert(v < size()); return leader(u) == leader(v); } std::vector> decompose() { std::vector> ret(size()); for (const usize u : rep(0, size())) { ret[leader(u)].push_back(u); } ret.erase(std::remove_if( ret.begin(), ret.end(), [&](const std::vector& v) { return v.empty(); }), ret.end()); return ret; } }; template bool setmin(T& lhs, const T& rhs) { if (lhs > rhs) { lhs = rhs; return true; } return false; } template using Vec = std::vector; void E_main() { usize N, M; std::cin >> N >> M; Vec> edge(M); for (auto& [d, s, t] : edge) { std::cin >> s >> t >> d; s -= 1; t -= 1; } std::sort(edge.rbegin(), edge.rend()); Vec> graph(N); UnionFind dsu(N); u32 w = 0; for (const auto [d, s, t] : edge) { dsu.merge(s, t); if (dsu.same(0, N - 1)) { w = d; std::cout << d << ' '; break; } } for (const auto [d, s, t] : edge) { if (d >= w) { graph[s].push_back(t); graph[t].push_back(s); } } Vec dist(N, N); std::queue que; const auto push = [&](const usize u, const usize d) { if (setmin(dist[u], d)) { que.push(u); } }; push(0, 0); while (!que.empty()) { const auto u = que.front(); que.pop(); for (const auto v : graph[u]) { push(v, dist[u] + 1); } } std::cout << dist[N - 1] << '\n'; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); E_main(); return 0; }