use std::{collections::*, io::Read}; 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 main() { solve(std::io::stdin().lock()) } 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 mut visited = vec![(0, 0); n as usize]; let mut que = BinaryHeap::<(i32, i32, usize)>::new(); que.push((i32::max_value(), 0, 0)); visited[0] = (i32::max_value(), 0); while let Some((w, d, v)) = que.pop() { // let d = visited[v].1; // if v as i32 == n - 1 { // println!("{} {}", w, d); // return; // } if visited[v].0 > w { continue; } for (v2, cap) in graph.to(v).iter() { let w2 = w.min(*cap); // println!(" {} >={} : ({}->{})", visited[*v2].0, w2, v, *v2); if visited[*v2].0 >= w2 { continue; } visited[*v2] = (w2, d + 1); que.push((w2, d + 1, *v2)); } } println!( "{} {}", visited[(n - 1) as usize].0, visited[(n - 1) as usize].1 ); // panic!("non-connective"); }