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::collections::VecDeque; input!(n: usize, m: usize); let mut links: Vec> = vec![vec![]; n]; for _i in 0..m { input!(s: usize, t: usize, d: i64); let s = s - 1; let t = t - 1; links[s].push((t, d)); links[t].push((s, d)); } let mut que: VecDeque<(usize, i64)> = VecDeque::new(); que.push_back((0, i64::MAX)); let mut dp = vec![(-1, -1); n]; dp[0] = (i64::MAX, 0); while !que.is_empty() { let (cp, mw) = que.pop_front().unwrap(); if dp[cp].0 > mw { continue; } for i in 0..links[cp].len() { let (np, lw) = links[cp][i]; let (w, s) = dp[np]; let nw = std::cmp::min(mw, lw); if w >= nw { continue; } if w == nw { dp[np] = (nw, std::cmp::min(s, dp[cp].1 + 1)); } else { dp[np] = (nw, dp[cp].1 + 1); } que.push_back((np, nw)); } } println!("{} {}", dp[n-1].0, dp[n-1].1); }