use std::io::Read; use std::io::Write; use std::cmp::*; #[derive(Eq, PartialEq)] struct State { v: usize, d: u64, } impl Ord for State { fn cmp(&self, rhs: &Self) -> Ordering { rhs.d.cmp(&self.d) } } impl PartialOrd for State { fn partial_cmp(&self, rhs: &Self) -> Option { Some(self.cmp(rhs)) } } fn run() { let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace(); let n: usize = it.next().unwrap().parse().unwrap(); let m: usize = it.next().unwrap().parse().unwrap(); let mut g = vec![vec![]; 2 * n]; for _ in 0..m { let a = it.next().unwrap().parse::().unwrap() - 1; let b = it.next().unwrap().parse::().unwrap() - 1; let c = it.next().unwrap().parse::().unwrap(); g[a].push((b, c)); g[b].push((a, c)); g[a].push((b + n, 0)); g[b].push((a + n, 0)); g[a + n].push((b + n, c)); g[b + n].push((a + n, c)); } let inf = std::u64::MAX; let mut dp = vec![inf; 2 * n]; dp[0] = 0; dp[n] = 0; let mut h = std::collections::BinaryHeap::new(); h.push(State{v: 0, d: 0}); while let Some(s) = h.pop() { if s.d > dp[s.v] { continue; } for &(u, w) in &g[s.v] { let d = s.d + w; if dp[u] > d { dp[u] = d; h.push(State{v: u, d: d}); } } } for i in 0..n { let d = dp[i] + dp[i + n]; writeln!(out, "{}", d).ok(); } } fn main() { run(); }