/* -*- coding: utf-8 -*- * * 1473.cc: No.1473 おでぶなおばけさん - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int INF = 1 << 30; /* typedef */ typedef queue qi; typedef pair pii; typedef vector vpii; /* global variables */ vpii nbrs[MAX_N]; int ds[MAX_N]; /* subroutines */ bool bfs(int st, int gl, int w) { memset(ds, -1, sizeof(ds)); ds[st] = 0; qi q; q.push(st); while (! q.empty()) { int u = q.front(); q.pop(); if (u == gl) return true; int vd = ds[u] + 1; vpii nbru = nbrs[u]; for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int v = vit->first, d = vit->second; if (ds[v] < 0 && w <= d) { ds[v] = vd; q.push(v); } } } return false; } /* main */ int main() { int n, m; scanf("%d%d", &n, &m); int mind = INF, maxd = 0; for (int i = 0; i < m; i++) { int u, v, d; scanf("%d%d%d", &u, &v, &d); u--, v--; nbrs[u].push_back(pii(v, d)); nbrs[v].push_back(pii(u, d)); mind = min(mind, d); maxd = max(maxd, d); } //printf("mind=%d, maxd=%d\n", mind, maxd); int w0 = mind, w1 = maxd + 1; while (w0 + 1 < w1) { int w = (w0 + w1) / 2; if (bfs(0, n - 1, w)) w0 = w; else w1 = w; } bfs(0, n - 1, w0); printf("%d %d\n", w0, ds[n - 1]); return 0; }