package yukicoder; import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.util.Arrays; public class No1473_3 { private static class Q { int i; int d; public Q(int i, int d) { this.i = i; this.d = d; } } private static int n; private static List> V; public static void main(String[] args) { Scanner scan = new Scanner(System.in); n = scan.nextInt(); int m = scan.nextInt(); V = new ArrayList>(); for (int i = 0; i < n; i++) { V.add(new ArrayList()); } int max = 0; int min = Integer.MAX_VALUE; for (int i=0; i < m; i++) { int s = scan.nextInt() - 1; int t = scan.nextInt() - 1; int d = scan.nextInt(); V.get(s).add(new Q(t, d)); V.get(t).add(new Q(s, d)); max = Math.max(max, d); min = Math.min(min, d); } scan.close(); for (int i=0; i < n; i++) { V.get(i).sort((a,b) -> b.d - a.d); } Q l = new Q(min-1, 0), r = new Q(max+1, 0); while (r.i - l.i > 1) { int mid = (r.i + l.i) / 2; int c = bfs(mid); if (c < 0) { r.i = mid; r.d = 0; } else { l.i = mid; l.d = c; } } System.out.println(String.format("%d %d", l.i,l.d)); } private static int bfs(int w) { int[] dist = new int[n]; Arrays.fill(dist, -1); List que = new ArrayList(); dist[0] = 0; que.add(0); while (!que.isEmpty()) { int v = que.remove(0); for (Q nv : V.get(v)) { if (nv.d >= w) { if (dist[nv.i] < 0) { dist[nv.i] = dist[v] + 1; if (nv.i == n - 1) { return dist[nv.i]; } que.add(nv.i); } } } } return dist[n-1]; } }