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 Edge { to: usize, cost: u64, } impl PartialOrd for Edge { fn partial_cmp(&self, other: &Self) -> Option { other.cost.partial_cmp(&self.cost) } } impl Ord for Edge { fn cmp(&self, other: &Self) -> Ordering { other.cost.cmp(&self.cost) } } input!(n: usize, m: usize); // println!("n:{}, m:{}", n, m); 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 - 1].push(Edge { to: t - 1, cost: d }); cities[t - 1].push(Edge { to: s - 1, cost: d }); } for i in 0..cities.len() { for j in 0..cities[i].len() { cities[i][j].cost = input_d_max - cities[i][j].cost; } } // use std::cmp::Reverse; use std::collections::BinaryHeap; let mut que = BinaryHeap::new(); que.push((0, 0)); let mut dist = vec![u64::MAX; n]; dist[0] = 0; let mut route: Vec<(Option, Option)> = vec![(None, None); n]; while !que.is_empty() { if let Some((_cost, this_index)) = que.pop() { for i in 0..cities[this_index].len() { let to = cities[this_index][i].to; if dist[to] > dist[this_index] + cities[this_index][i].cost { dist[to] = dist[this_index] + cities[this_index][i].cost; que.push((dist[to], to)); route[to] = (Some(input_d_max - dist[to]), Some(this_index)); } } } } // dbg!(&cities); // dbg!(&dist); // dbg!(&route); let mut w = u64::MAX; let mut count = 0; let mut current = Some(n-1); while current.is_some() { if let (Some(w1), Some(from)) = route[current.unwrap()] { current = Some(from); if w1 < w { w = w1; } count += 1; } else { current = None; } } println!("{} {}", w, count); }