package yukicoder; import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class No1473_2 { 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_d = 0; 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 Integer[] {t, d}); V.get(t).add(new Integer[] {s, d}); max_d = Math.max(max_d, d); } int[] l = new int[] {0, 0}, r = new int[] {max_d + 1, 0}; while (r[0] - l[0] > 1) { int mid = (r[0] + l[0]) / 2; int c = bfs(mid); if (c == 0) { r[0] = mid; r[1] = 0; } else { l[0] = mid; l[1] = c; } } System.out.println(String.format("%d %d", l[0],l[1])); } private static int bfs(int w) { boolean[] B = new boolean[n]; List q = new ArrayList(); q.add(new Integer[] {0, 0}); while (q.size() > 0) { Integer[] v = q.remove(0); if (v[0] == n-1) { return v[1]; } if (B[v[0]]) { continue; } B[v[0]] = true; for (Integer[] i : V.get(v[0])) { if (i[1] >= w) { q.add(new Integer[] {i[0], v[1] + 1}); } } } return 0; } }