#include #include #include #include using namespace std; struct edge{ int s,t,d; }; bool operator<(edge a, edge b){ if(a.d == b.d){ if(a.s == b.s){ return a.t < b.t; } return a.s < b.s; } return a.d < b.d; } bool operator==(edge a, edge b){ return a.s == b.s && a.t == b.t && a.d == b.d; } bool operator>(edge a,edge b){ return !(a par; vector size_; UnionFind(int n_) : n(n_), par((size_t)n_), size_((size_t)n_,1){ for(int i = 0; n > i; i++)par[i] = i; } int root(int x){ if(par[x] == x)return x; return par[x] = root(par[x]); } void unite(int a,int b){ int ra = root(a); int rb = root(b); if(ra==rb)return; if(size(ra) > size(rb)) swap(ra,rb); par[ra] = rb; size_[rb] += size_[ra]; } bool same(int a, int b){ return root(a) == root(b); } int size(int a){ return size_[root(a)]; } void debug(){ for(int i = 0; n > i; i++){ cout << size_[root(i)] << " "; } cout << endl; return; } }; template class dijkstra{ struct vg{ int t; T c; }; vector cost; void run(int x){ auto comp = [](vg a,vg b){ return a.c>b.c; }; priority_queue,decltype(comp)> R{comp}; for(int i = 0; n > i; i++){ cost[i] = -1; } cost[x] = 0; R.push({x,0}); while(R.size()){ auto k = R.top();R.pop(); if(cost[k.t] != k.c)continue; for(int i = 0; A[k.t].size() > i; i++){ if(cost[A[k.t][i].t] != -1 && cost[A[k.t][i].t] <= k.c+A[k.t][i].c)continue; cost[A[k.t][i].t] = k.c+A[k.t][i].c; R.push({A[k.t][i].t,k.c+A[k.t][i].c}); } } } public: int n; vector> A; dijkstra(int n_):n(n_),A(n_),cost(n_,-1){} //双方向 void push(int s,int v){ A[s].push_back({v,1}); A[v].push_back({s,1}); } void push(int s,int v,int c){ A[s].push_back({v,c}); A[v].push_back({s,c}); } void push_side(int s,int v,int c){ A[s].push_back({v,c}); } void build(int x){ run(x); } int operator[](int i){ return cost[i]; } };int main(){ int n,m;cin>>n>>m; vector A(m); for(int i = 0; m > i; i++){ cin>>A[i].s>>A[i].t>>A[i].d; A[i].s--;A[i].t--; } sort(A.begin(),A.end(),greater()); UnionFind B(n); dijkstra C(n); int rew = -1; for(int i = 0; m > i; i++){ if(rew > A[i].d)break; C.push(A[i].s,A[i].t); B.unite(A[i].s,A[i].t); if(B.same(0,n-1)){ rew = A[i].d; } } C.build(0); cout << rew << " " << C[n-1] << endl; }