#![allow(unused_imports, unused_macros, dead_code)] use std::{cmp::*, collections::*}; fn main() { let mut sc = Scanner::new(); let n: usize = sc.cin(); let m: usize = sc.cin(); let mut d = vec![vec![Hyper::Inf; n]; n]; for i in 0..n { d[i][i] = Hyper::zero(); } for _ in 0..m { let s = sc.cin::() - 1; let t = sc.cin::() - 1; let w: i64 = sc.cin(); d[s][t] = min!(d[s][t], Hyper::Real(w)); // d[t][s] = Hyper::Real(w); } warshall_floyd(&mut d); for i in 0..n { let a: i64 = d[i] .iter() .map(|&x| match x { Hyper::Real(x) => x, _ => 0, }) .sum(); put!(a); } } // @graph/shortest/warshall_floyd // @algebra/group pub trait Group: std::ops::Add + std::ops::Sub + std::ops::Neg + std::iter::Sum { fn zero() -> Self; } macro_rules! define_group { ($t:ty, $x:expr) => { impl Group for $t { fn zero() -> Self { $x } } }; } define_group!(i32, 0); define_group!(i64, 0); define_group!(i128, 0); define_group!(f32, 0.0); define_group!(f64, 0.0); // @algebra/hyper #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum Hyper { NegInf, Real(X), Inf, } use Hyper::{Inf, NegInf, Real}; impl Hyper { pub fn unwrap(self) -> X { if let Hyper::Real(x) = self { x } else { panic!() } } } impl std::ops::Add for Hyper { type Output = Self; fn add(self, rhs: Hyper) -> Hyper { match (self, rhs) { (Real(x), Real(y)) => Real(x + y), (Inf, _) => Inf, (_, Inf) => Inf, _ => NegInf, } } } impl std::ops::Sub for Hyper { type Output = Self; fn sub(self, rhs: Hyper) -> Hyper { self + (-rhs) } } impl std::ops::Neg for Hyper { type Output = Self; fn neg(self) -> Hyper { match self { Inf => NegInf, NegInf => Inf, Real(x) => Real(-x), } } } impl std::iter::Sum for Hyper { fn sum>(iter: I) -> Self { iter.fold(Hyper::zero(), std::ops::Add::add) } } impl Group for Hyper { fn zero() -> Self { Hyper::Real(X::zero()) } } impl std::ops::Add for Hyper { type Output = Self; fn add(self, other: X) -> Self { match self { Inf => Inf, NegInf => NegInf, Real(x) => Real(x + other), } } } pub fn warshall_floyd(d: &mut [Vec>]) { let n = d.len(); for i in 0..n { d[i][i] = Hyper::::zero(); } for k in 0..n { for i in 0..n { for j in 0..n { let w = d[i][k] + d[k][j]; if w < d[i][j] { d[i][j] = w; } } } } } // {{{ use std::io::{self, Write}; use std::str::FromStr; struct Scanner { stdin: io::Stdin, buffer: VecDeque, } impl Scanner { fn new() -> Self { Self { stdin: io::stdin(), buffer: VecDeque::new(), } } fn cin(&mut self) -> T { while self.buffer.is_empty() { let mut line = String::new(); let _ = self.stdin.read_line(&mut line); for w in line.split_whitespace() { self.buffer.push_back(String::from(w)); } } self.buffer.pop_front().unwrap().parse::().ok().unwrap() } fn chars(&mut self) -> Vec { self.cin::().chars().collect() } fn vec(&mut self, n: usize) -> Vec { (0..n).map(|_| self.cin()).collect() } } fn flush() { std::io::stdout().flush().unwrap(); } #[macro_export] macro_rules! min { (.. $x:expr) => {{ let mut it = $x.iter(); it.next().map(|z| it.fold(z, |x, y| min!(x, y))) }}; ($x:expr) => ($x); ($x:expr, $($ys:expr),*) => {{ let t = min!($($ys),*); if $x < t { $x } else { t } }} } #[macro_export] macro_rules! max { (.. $x:expr) => {{ let mut it = $x.iter(); it.next().map(|z| it.fold(z, |x, y| max!(x, y))) }}; ($x:expr) => ($x); ($x:expr, $($ys:expr),*) => {{ let t = max!($($ys),*); if $x > t { $x } else { t } }} } #[macro_export] macro_rules! trace { ($x:expr) => { #[cfg(debug_assertions)] eprintln!(">>> {} = {:?}", stringify!($x), $x) }; ($($xs:expr),*) => { trace!(($($xs),*)) } } #[macro_export] macro_rules! put { (.. $x:expr) => {{ let mut it = $x.iter(); if let Some(x) = it.next() { print!("{}", x); } for x in it { print!(" {}", x); } println!(""); }}; ($x:expr) => { println!("{}", $x) }; ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) } } // }}}