use std::{collections::*, io::Read, ops::Sub}; fn take_token(cin: &mut R) -> String { cin.bytes() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::() } #[allow(unused)] macro_rules! scan { ($io:expr => $t:ty) => (take_token(&mut $io).parse::<$t>().unwrap()); ($io:expr => $t:tt * $n:expr) => ((0..$n).map(|_| scan!($io => $t)).collect::>()); ($io:expr => $($t:tt),*) => (($(scan!($io => $t)),*)); ($io:expr => $($t:tt),* * $n:expr) => ((0..$n).map(|_| ($(scan!($io => $t)),*)).collect::>()); } struct Graph(Vec>); impl Graph { fn to<'a>(&self, i: usize) -> &Vec<(usize, T)> { &(self.0)[i] } } struct GraphBuilder(Graph); impl GraphBuilder { fn new(n: usize) -> Self { Self(Graph::(vec![Vec::<(usize, T)>::new(); n])) } fn connect(&mut self, u: usize, v: usize, item: T) { (self.0 .0)[u].push((v, item.clone())); (self.0 .0)[v].push((u, item)); } fn build(self) -> Graph { self.0 } } fn binary_search bool>(ok_val: i64, ng_val: i64, check: F) -> i64 { let mut ok = ok_val; let mut ng = ng_val; while (ok - ng).abs() > 1 { let m = (ok + ng) / 2; if check(m) { ok = m; } else { ng = m; } } ok } fn main() { solve(std::io::stdin().lock()) } fn calc(graph: &Graph, n: usize, k: i32) -> Option { let mut visited = vec![0; n as usize]; let mut que = VecDeque::::new(); que.push_back(0); while let Some(v) = que.pop_front() { let d = visited[v]; if v == n - 1 { return Some(d); } for (v2, cap) in graph.to(v).iter() { if *cap < k || visited[*v2] > 0 { continue; } visited[*v2] = d + 1; que.push_back(*v2); } } None } fn solve(mut cin: R) { let (n, m) = scan!(cin => i32, i32); let mut builder = GraphBuilder::::new(n as usize); for _ in 0..m { let (u, v, m) = scan!(cin => i32, i32, i32); builder.connect((u - 1) as usize, (v - 1) as usize, m); } let graph = builder.build(); let k = binary_search(0, (2e9) as i64, |m| { calc(&graph, n as usize, m as i32).is_some() }) as i32; println!("{} {}", k, calc(&graph, n as usize, k).unwrap()); }