use std::io::Read; use std::collections::VecDeque; use std::cmp; struct TourPlan { spot: usize, cost: usize, max_cost: usize, } fn bfs(st_spot: usize, spots: &Vec>) -> usize { let mut min_cost: usize = std::usize::MAX; let mut ticketuse_cost: usize = std::usize::MAX; let mut queue: VecDeque = VecDeque::new(); queue.push_back( TourPlan { spot: st_spot, cost: 0, max_cost: 0 } ); while !queue.is_empty() { let tp = queue.pop_front().unwrap(); for &(s, c) in &spots[tp.spot] { let max_cost = cmp::max(tp.max_cost, c); if s == 1 { min_cost = cmp::min(min_cost, tp.cost+c); ticketuse_cost = cmp::min(ticketuse_cost, tp.cost+c - max_cost); } else { let cost = tp.cost + c; if cost > min_cost && cost - max_cost > ticketuse_cost { break; } queue.push_back( TourPlan { spot: s, cost, max_cost } ); } } } min_cost + ticketuse_cost } fn main() { let mut buf = String::new(); let stdin = std::io::stdin(); stdin.lock().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let n = iter.next().unwrap().parse::().unwrap(); let m = iter.next().unwrap().parse::().unwrap(); // spots[出発地] = [(目的地, cost), ...] let mut spots: Vec> = vec![vec![]; n+1]; for _ in 0..m { let a = iter.next().unwrap().parse::().unwrap(); let b = iter.next().unwrap().parse::().unwrap(); let c = iter.next().unwrap().parse::().unwrap(); spots[a].push((b, c)); spots[b].push((a, c)); } let mut ans = String::new(); ans.push_str("0\n"); for i in 2..n+1 { let ans_i = bfs(i, &spots); ans.push_str(&format!("{}\n", ans_i)); } print!("{}", ans); }