#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace atcoder; //* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *// void input() { } template struct graph_l { public : graph_l(int n) : _n(n), _graph(n), _dist(n, 0) {} void add (int from, int to, Cost cost){ assert(0 <= from && from < _n); assert(0 <= to && to < _n); _graph[from].push_back(_edge{(unsigned int)from, (unsigned int)to, cost}); } void add_bi (int from, int to, Cost cost){ add(from, to, cost); add(to, from, cost); } Cost get_dist (int v) { assert(0 <= v && v < _n); return _dist[v]; } void dijkstra (int s) { assert(0 <= s && s < _n); for (int i = 0 ; i < _n ; ++i) { _dist[i] = DIST_INF; } auto edge_compare = [] (_edge e1, _edge e2) { if (e1.cost != e2.cost) return e1.cost > e2.cost; else if (e1.to != e2.to) return e1.to > e2.to; else return e1.from > e2.from; }; priority_queue<_edge, vector<_edge>, function> edge_que(edge_compare); for (auto e : _graph[s]) { edge_que.push(_edge{e.from, e.to, e.cost}); if(_dist[e.to] > e.cost) _dist[e.to] = e.cost; } while (!edge_que.empty()) { _edge p = edge_que.top(); edge_que.pop(); unsigned int v = p.to; if (_dist[v] < p.cost) continue; for (auto e : _graph[v]) { auto next_cost = e.cost + _dist[v]; if (_dist[e.to] <= next_cost) continue; _dist[e.to] = next_cost; edge_que.push(_edge{e.from, e.to, _dist[e.to]}); } } } private: int _n; const Cost DIST_INF = numeric_limits::max(); struct _edge { unsigned int from; unsigned int to; Cost cost; }; std::vector> _graph; std::vector _dist; }; void solve() { using pii = pair; int N, M; cin >> N >> M; vector S(M), T(M); vector D(M); for(int i = 0; i < M; i++) { int s, t, d; cin >> s >> t >> d; s--; t--; S[i] = s; T[i] = t; D[i] = {d, i}; } auto compare = [](pii a, pii b) -> bool { if(a.first != b.first) return a.first > b.first; else return a.second < b.second; }; sort(D.begin(), D.end(), compare); int w = 0; dsu UF(N); graph_l G(N); for(int i = 0; i < M; i++) { int wi = D[i].first; int ni = D[i].second; UF.merge(S[ni], T[ni]); if(w > 0 && w > wi) break; if(w == 0 && UF.same(0, N-1)) { w = wi; } G.add_bi(S[ni], T[ni], 1); } G.dijkstra(0); cout << w << " " << G.get_dist(N-1) << endl; } //* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *// int main() { std::ifstream in("input.txt"); std::cin.rdbuf(in.rdbuf()); std::cin.tie(0); ios::sync_with_stdio(false); input(); solve(); return 0; }