use std::{cmp::Reverse, collections::BinaryHeap}; const INF: usize = 1usize << 60; fn dijkstra(start: usize, paths: &Vec>) -> Vec> { let n = paths.len(); let mut que = BinaryHeap::new(); que.push((Reverse(0), start, 0)); let mut costs = vec![vec![INF; 2]; n]; costs[start][0] = 0; costs[start][1] = 0; while let Some((Reverse(cost), u, depth)) = que.pop() { if cost > costs[u][depth] { continue; } for &(v, w) in paths[u].iter() { if costs[u][depth] + w < costs[v][depth] { costs[v][depth] = costs[u][depth] + w; que.push((Reverse(costs[v][depth]), v, depth)); } if depth == 0 && costs[u][0] < costs[v][1] { costs[v][1] = costs[u][0]; que.push((Reverse(costs[v][1]), v, 1)); } } } costs } fn main() { let mut nm = String::new(); std::io::stdin().read_line(&mut nm).ok(); let nm: Vec = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nm[0]; let m = nm[1]; let mut paths = vec![vec![]; n]; for _ in 0..m { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let a = temp[0]-1; let b = temp[1]-1; let c = temp[2]; paths[a].push((b, c)); paths[b].push((a, c)); } let dists = dijkstra(0, &paths); for i in 0..n { println!("{}", dists[i].iter().sum::()); } }