#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; //using namespace atcoder; #define REP(i, n) for (int i=0; i<(n); ++i) #define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i) #define FOR(i, a, n) for (int i=(a); i<(n); ++i) #define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #define DUMP(x) cerr<<#x<<" = "<<(x)< ostream &operator<<(ostream &os, const vector &v) { os << "["; REP(i, SZ(v)) { if (i) os << ", "; os << v[i]; } return os << "]"; } template ostream &operator<<(ostream &os, const pair &p) { return os << "(" << p.first << " " << p.second << ")"; } template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } using ll = long long; using ull = unsigned long long; using ld = long double; using P = pair; using vi = vector; using vll = vector; using vvi = vector; using vvll = vector; const ll MOD = 1e9 + 7; const int INF = INT_MAX / 2; const ll LINF = LLONG_MAX / 2; const ld eps = 1e-9; // edit struct UnionFind { vector par, sz; UnionFind(int n) : par(n), sz(n, 1) { for (int i = 0; i < n; ++i) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void merge(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (sz[x] < sz[y]) swap(x, y); par[y] = x; sz[x] += sz[y]; sz[y] = 0; } bool issame(int x, int y) { return root(x) == root(y); } int size(int x) { return sz[root(x)]; } }; template class graph { struct edge { int src, to; T cost; }; public: int n; vector> edges; graph(int n_) : n(n_), edges(n_) { } void add_edge(int src, int to, T cost) { edges[src].push_back({src, to, cost}); } }; template pair, vector> dijkstra(const graph &g, int s, T inf) { vector d(g.edges.size(), inf); vector p(g.edges.size()); using Pi = pair; priority_queue, greater> que; que.emplace(0, s); d[s] = 0; p[s] = -1; while (!que.empty()) { T cost; int v; tie(cost, v) = que.top(); que.pop(); if (d[v] < cost) continue; for (const auto &e : g.edges[v]) { T ncost = cost + e.cost; int nv = e.to; if (ncost < d[nv]) { d[nv] = ncost; p[nv] = v; que.emplace(ncost, nv); } } } return make_pair(d, p); } void solve() { int n, m; cin >> n >> m; vector> E(m); REP(i, m) { int s, t; ll d; cin >> s >> t >> d; s--, t--; E[i] = make_tuple(s, t, d); } sort(ALL(E), [](auto l, auto r) { return get<2>(l) > get<2>(r); }); UnionFind uf(n); graph g(n); int max_weight = 0; for (int i = 0; i < m; ++i) { if (uf.issame(0, n - 1) && get<2>(E[i - 1]) != get<2>(E[i])) { break; } max_weight = get<2>(E[i]); int s, t; ll d; tie(s, t, d) = E[i]; uf.merge(s, t); g.add_edge(s, t, 1); g.add_edge(t, s, 1); } auto dist = dijkstra(g, 0, INF).first; int ans = dist[n - 1]; cout << max_weight << " " << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); // std::ifstream in("input.txt"); // std::cin.rdbuf(in.rdbuf()); solve(); return 0; }