macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => { let mut $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } fn main() { use std::cmp::Ordering; #[derive(Clone, Eq, PartialEq, Debug)] struct Node { to: usize, max: u64, } impl PartialOrd for Node { fn partial_cmp(&self, other: &Self) -> Option { other.max.partial_cmp(&self.max) } } impl Ord for Node { fn cmp(&self, other: &Self) -> Ordering { other.max.cmp(&self.max) } } input!(n: usize, m: usize); let n = n + 1; let mut cities: Vec> = vec![vec![]; n]; let mut input_d_max = 0; for _i in 0..m { input!(s: usize, t: usize, d: u64); if input_d_max < d { input_d_max = d; } cities[s].push(Node { to: t, max: d, }); cities[t].push(Node { to: s, max: d, }); } for i in 0..n { cities[i].sort_by(|a, b| b.max.partial_cmp(&a.max).unwrap() ); } use std::collections::VecDeque; let mut que = VecDeque::new(); que.push_front(1); let mut dist = vec![None; n]; dist[1] = Some(0u64); let mut weight = 0; while !que.is_empty() { if let Some(this_index) = que.pop_front() { for i in 0..cities[this_index].len() { let to = cities[this_index][i].to; if dist[to].is_some() { continue; } dist[to] = Some(dist[this_index].unwrap_or(0) + 1); que.push_front(to); if weight == 0 { weight = cities[this_index][i].max; } else if weight > cities[this_index][i].max { weight = cities[this_index][i].max; } break; } } } println!("{} {}", weight, dist[n-1].unwrap()); }