結果

問題 No.1344 Typical Shortest Path Sum
ユーザー cympfhcympfh
提出日時 2021-01-16 14:41:35
言語 Rust
(1.77.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 5,175 bytes
コンパイル時間 1,145 ms
コンパイル使用メモリ 154,708 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-18 11:52:40
合計ジャッジ時間 3,836 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,384 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,384 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 5 ms
4,376 KB
testcase_41 AC 5 ms
4,376 KB
testcase_42 AC 4 ms
4,380 KB
testcase_43 AC 5 ms
4,380 KB
testcase_44 AC 4 ms
4,380 KB
testcase_45 AC 5 ms
4,376 KB
testcase_46 AC 5 ms
4,388 KB
testcase_47 AC 4 ms
4,384 KB
testcase_48 AC 5 ms
4,376 KB
testcase_49 AC 5 ms
4,380 KB
testcase_50 AC 4 ms
4,380 KB
testcase_51 AC 4 ms
4,380 KB
testcase_52 AC 4 ms
4,376 KB
testcase_53 AC 5 ms
4,380 KB
testcase_54 AC 4 ms
4,376 KB
testcase_55 AC 5 ms
4,376 KB
testcase_56 AC 4 ms
4,376 KB
testcase_57 AC 4 ms
4,376 KB
testcase_58 AC 5 ms
4,380 KB
testcase_59 AC 5 ms
4,376 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 1 ms
4,380 KB
testcase_64 AC 1 ms
4,376 KB
testcase_65 AC 1 ms
4,376 KB
testcase_66 AC 2 ms
4,380 KB
testcase_67 AC 2 ms
4,376 KB
testcase_68 AC 1 ms
4,380 KB
testcase_69 AC 1 ms
4,376 KB
testcase_70 AC 1 ms
4,376 KB
testcase_71 AC 1 ms
4,380 KB
testcase_72 AC 1 ms
4,380 KB
testcase_73 AC 1 ms
4,376 KB
testcase_74 AC 1 ms
4,380 KB
testcase_75 AC 1 ms
4,384 KB
testcase_76 AC 1 ms
4,380 KB
testcase_77 AC 1 ms
4,380 KB
testcase_78 AC 1 ms
4,380 KB
testcase_79 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![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::<usize>() - 1;
        let t = sc.cin::<usize>() - 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<Output = Self>
    + std::ops::Sub<Output = Self>
    + std::ops::Neg<Output = Self>
    + 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<X> {
    NegInf,
    Real(X),
    Inf,
}
use Hyper::{Inf, NegInf, Real};
impl<X> Hyper<X> {
    pub fn unwrap(self) -> X {
        if let Hyper::Real(x) = self {
            x
        } else {
            panic!()
        }
    }
}
impl<X: Group> std::ops::Add for Hyper<X> {
    type Output = Self;
    fn add(self, rhs: Hyper<X>) -> Hyper<X> {
        match (self, rhs) {
            (Real(x), Real(y)) => Real(x + y),
            (Inf, _) => Inf,
            (_, Inf) => Inf,
            _ => NegInf,
        }
    }
}
impl<X: Group> std::ops::Sub for Hyper<X> {
    type Output = Self;
    fn sub(self, rhs: Hyper<X>) -> Hyper<X> {
        self + (-rhs)
    }
}
impl<X: Group> std::ops::Neg for Hyper<X> {
    type Output = Self;
    fn neg(self) -> Hyper<X> {
        match self {
            Inf => NegInf,
            NegInf => Inf,
            Real(x) => Real(-x),
        }
    }
}
impl<X: Group> std::iter::Sum for Hyper<X> {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Hyper::zero(), std::ops::Add::add)
    }
}
impl<X: Group> Group for Hyper<X> {
    fn zero() -> Self {
        Hyper::Real(X::zero())
    }
}
impl<X: Group> std::ops::Add<X> for Hyper<X> {
    type Output = Self;
    fn add(self, other: X) -> Self {
        match self {
            Inf => Inf,
            NegInf => NegInf,
            Real(x) => Real(x + other),
        }
    }
}

pub fn warshall_floyd<X: Copy + Group + PartialOrd>(d: &mut [Vec<Hyper<X>>]) {
    let n = d.len();
    for i in 0..n {
        d[i][i] = Hyper::<X>::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<String>,
}
impl Scanner {
    fn new() -> Self {
        Self {
            stdin: io::stdin(),
            buffer: VecDeque::new(),
        }
    }
    fn cin<T: FromStr>(&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::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> {
        self.cin::<String>().chars().collect()
    }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (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),*) }
}
// }}}
0