結果

問題 No.468 役に立つ競技プログラミング実践編
ユーザー hatoohatoo
提出日時 2017-10-20 18:15:30
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 157 ms / 2,000 ms
コード長 3,765 bytes
コンパイル時間 15,430 ms
コンパイル使用メモリ 378,756 KB
実行使用メモリ 35,040 KB
最終ジャッジ日時 2024-11-21 10:49:30
合計ジャッジ時間 17,090 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 1 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 1 ms
5,248 KB
testcase_10 AC 1 ms
5,248 KB
testcase_11 AC 1 ms
5,248 KB
testcase_12 AC 1 ms
5,248 KB
testcase_13 AC 1 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 2 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 142 ms
30,908 KB
testcase_25 AC 145 ms
30,964 KB
testcase_26 AC 157 ms
30,924 KB
testcase_27 AC 145 ms
31,032 KB
testcase_28 AC 157 ms
30,812 KB
testcase_29 AC 145 ms
30,912 KB
testcase_30 AC 146 ms
30,848 KB
testcase_31 AC 144 ms
30,884 KB
testcase_32 AC 145 ms
30,936 KB
testcase_33 AC 143 ms
30,800 KB
testcase_34 AC 67 ms
35,040 KB
testcase_35 AC 1 ms
5,248 KB
testcase_36 AC 1 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn get<T: FromStr>() -> T
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }

    #[allow(dead_code)]
    pub fn get2<T: FromStr, U: FromStr>() -> (T, U)
    where
        <T as FromStr>::Err: Debug,
        <U as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    }

    #[allow(dead_code)]
    pub fn get3<S: FromStr, T: FromStr, U: FromStr>() -> (S, T, U)
    where
        <S as FromStr>::Err: Debug,
        <T as FromStr>::Err: Debug,
        <U as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    }
}

// std::cmp::Reverse doesn't have Clone.
#[derive(Eq, PartialEq, Clone)]
struct Rev<T>(pub T);

impl<T: PartialOrd> PartialOrd for Rev<T> {
    fn partial_cmp(&self, other: &Rev<T>) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Rev<T>) -> Ordering {
        other.0.cmp(&self.0)
    }
}

#[allow(unused_macros)]
macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn dfs(
    i: usize,
    edges_rev: &Vec<Vec<(usize, usize)>>,
    memo: &mut HashMap<usize, usize>,
    path: &mut Vec<Vec<usize>>,
) -> usize {

    if let Some(&t) = memo.get(&i) {
        return t;
    }

    let l = edges_rev[i]
        .iter()
        .map(|&(a, c)| (dfs(a, edges_rev, memo, path) + c, a))
        .collect::<Vec<_>>();

    let longest = l.iter().map(|t| t.0).max().unwrap_or(0);

    for &(_, a) in l.iter().filter(|t| t.0 == longest) {
        path[i].push(a);
    }

    memo.insert(i, longest);
    longest
}

fn dfs2(i: usize, critical_edges: &Vec<Vec<usize>>, visited: &mut [bool]) {
    if visited[i] {
        return;
    }
    visited[i] = true;
    for &a in &critical_edges[i] {
        dfs2(a, critical_edges, visited);
    }
}

fn main() {
    let (n, m): (usize, usize) = util::get2();
    let abc: Vec<(usize, usize, usize)> = (0..m).map(|_| util::get3()).collect();

    let mut edges_rev = vec![Vec::new(); n];
    let mut critical_edges = vec![Vec::new(); n];
    let mut memo = HashMap::new();
    let mut visited = vec![false; n];

    for &(a, b, c) in &abc {
        edges_rev[b].push((a, c));
    }

    let t = dfs(n - 1, &edges_rev, &mut memo, &mut critical_edges);
    dfs2(n - 1, &critical_edges, &mut visited);
    println!("{} {}/{}", t, n - visited.iter().filter(|&&b| b).count(), n);
}
0