結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー phsplsphspls
提出日時 2022-10-08 12:26:30
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,739 bytes
コンパイル時間 4,848 ms
コンパイル使用メモリ 147,540 KB
実行使用メモリ 24,964 KB
最終ジャッジ日時 2023-09-04 15:37:08
合計ジャッジ時間 13,880 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 28 ms
9,844 KB
testcase_24 AC 20 ms
4,548 KB
testcase_25 AC 63 ms
11,176 KB
testcase_26 AC 107 ms
14,000 KB
testcase_27 AC 73 ms
10,836 KB
testcase_28 AC 42 ms
9,072 KB
testcase_29 AC 66 ms
10,372 KB
testcase_30 AC 46 ms
9,060 KB
testcase_31 AC 34 ms
10,144 KB
testcase_32 AC 49 ms
9,764 KB
testcase_33 AC 99 ms
12,116 KB
testcase_34 AC 103 ms
14,656 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 48 ms
8,468 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 62 ms
23,960 KB
testcase_44 AC 36 ms
12,308 KB
testcase_45 AC 52 ms
22,996 KB
testcase_46 AC 12 ms
8,252 KB
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{collections::BinaryHeap, cmp::Reverse};

const MOD: usize = 1e9 as usize + 7;

fn topologycal_sort(paths: &Vec<Vec<usize>>) -> Vec<usize> {
    let n = paths.len();
    let mut ret = vec![];
    let mut ins = vec![0usize; n];
    for i in 0..n {
        for &v in paths[i].iter() {
            ins[v] += 1;
        }
    }
    let mut que = BinaryHeap::new();
    for i in 0..n {
        if ins[i] == 0 {
            que.push(Reverse(i));
        }
    }
    while let Some(Reverse(x)) = que.pop() {
        ret.push(x);
        for &v in paths[x].iter() {
            ins[v] -= 1;
            if ins[v] == 0 {
                que.push(Reverse(v));
            }
        }
    }
    ret
}

fn main() {
    let mut nm = String::new();
    std::io::stdin().read_line(&mut nm).ok();
    let nm: Vec<usize> = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nm[0];
    let m = nm[1];
    let mut paths_for_dag = vec![vec![]; n+1];
    let mut paths = vec![vec![]; n+1];
    for _ in 0..m {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let u = temp[0];
        let v = temp[1];
        let l = temp[2];
        let a = temp[3];
        paths_for_dag[u].push(v);
        paths[u].push((v, l, a));
    }

    let temp = topologycal_sort(&paths_for_dag);
    if temp.len() < n+1 {
        println!("INF");
        return;
    }
    let mut cnts = vec![0usize; n+1];
    for &u in temp.iter() {
        for &(v, l, a) in paths[u].iter() {
            cnts[v] += l * a + cnts[u];
            cnts[v] %= MOD;
        }
    }
    println!("{}", cnts[n]);
}
0