結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー koba-e964koba-e964
提出日時 2021-11-09 22:40:58
言語 Rust
(1.77.0)
結果
AC  
実行時間 551 ms / 2,000 ms
コード長 2,248 bytes
コンパイル時間 12,689 ms
コンパイル使用メモリ 380,452 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 21:29:44
合計ジャッジ時間 21,447 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 0 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 51 ms
5,376 KB
testcase_08 AC 69 ms
5,376 KB
testcase_09 AC 496 ms
5,376 KB
testcase_10 AC 87 ms
5,376 KB
testcase_11 AC 393 ms
5,376 KB
testcase_12 AC 150 ms
5,376 KB
testcase_13 AC 249 ms
5,376 KB
testcase_14 AC 11 ms
5,376 KB
testcase_15 AC 106 ms
5,376 KB
testcase_16 AC 152 ms
5,376 KB
testcase_17 AC 31 ms
5,376 KB
testcase_18 AC 26 ms
5,376 KB
testcase_19 AC 215 ms
5,376 KB
testcase_20 AC 9 ms
5,376 KB
testcase_21 AC 394 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 27 ms
5,376 KB
testcase_29 AC 285 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 46 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 357 ms
5,376 KB
testcase_34 AC 167 ms
5,376 KB
testcase_35 AC 42 ms
5,376 KB
testcase_36 AC 35 ms
5,376 KB
testcase_37 AC 8 ms
5,376 KB
testcase_38 AC 92 ms
5,376 KB
testcase_39 AC 13 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 244 ms
5,376 KB
testcase_44 AC 118 ms
5,376 KB
testcase_45 AC 551 ms
5,376 KB
testcase_46 AC 58 ms
5,376 KB
testcase_47 AC 231 ms
5,376 KB
testcase_48 AC 248 ms
5,376 KB
testcase_49 AC 26 ms
5,376 KB
testcase_50 AC 1 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 49 ms
5,376 KB
testcase_53 AC 26 ms
5,376 KB
testcase_54 AC 204 ms
5,376 KB
testcase_55 AC 202 ms
5,376 KB
testcase_56 AC 201 ms
5,376 KB
testcase_57 AC 428 ms
5,376 KB
testcase_58 AC 433 ms
5,376 KB
testcase_59 AC 428 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
use std::collections::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

const INF: i64 = 1 << 50;

fn main() {
    input! {
        t: i32,
        n: usize, m: usize,
        abc: [(usize1, usize1, i64); m],
    }
    let mut mi = INF;
    for i in 0..m {
        let mut g = vec![vec![]; n];
        for j in 0..m {
            if i == j { continue; }
            let (a, b, c) = abc[j];
            g[a].push((b, c));
            if t == 0 {
                g[b].push((a, c));
            }
        }
        let mut dist = vec![INF; n];
        let mut que = BinaryHeap::new();
        let (ai, bi, ci) = abc[i];
        que.push((Reverse(0), bi));
        while let Some((Reverse(d), v)) = que.pop() {
            if dist[v] <= d { continue; }
            dist[v] = d;
            for &(w, c) in &g[v] {
                que.push((Reverse(d + c), w));
            }
        }
        mi = min(mi, dist[ai] + ci);
    }
    println!("{}", if mi >= INF { -1 } else { mi });
}
0