結果

問題 No.2565 はじめてのおつかい
ユーザー Kyo_s_sKyo_s_s
提出日時 2023-11-27 16:46:52
言語 Rust
(1.77.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 2,353 bytes
コンパイル時間 1,467 ms
コンパイル使用メモリ 187,816 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2023-11-27 16:47:00
合計ジャッジ時間 7,523 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 53 ms
12,800 KB
testcase_04 AC 37 ms
12,800 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 26 ms
6,676 KB
testcase_07 AC 13 ms
6,676 KB
testcase_08 AC 14 ms
6,676 KB
testcase_09 AC 24 ms
6,676 KB
testcase_10 AC 16 ms
6,676 KB
testcase_11 AC 37 ms
8,576 KB
testcase_12 AC 10 ms
6,676 KB
testcase_13 AC 16 ms
6,676 KB
testcase_14 AC 26 ms
6,676 KB
testcase_15 AC 28 ms
6,676 KB
testcase_16 AC 31 ms
9,856 KB
testcase_17 AC 6 ms
6,676 KB
testcase_18 AC 10 ms
6,676 KB
testcase_19 AC 33 ms
7,808 KB
testcase_20 AC 29 ms
7,296 KB
testcase_21 AC 28 ms
7,296 KB
testcase_22 AC 16 ms
6,676 KB
testcase_23 AC 19 ms
6,676 KB
testcase_24 AC 4 ms
6,676 KB
testcase_25 AC 29 ms
7,296 KB
testcase_26 AC 19 ms
6,676 KB
testcase_27 AC 29 ms
7,680 KB
testcase_28 AC 32 ms
7,552 KB
testcase_29 AC 45 ms
9,088 KB
testcase_30 AC 31 ms
8,320 KB
testcase_31 AC 30 ms
7,552 KB
testcase_32 AC 17 ms
6,676 KB
testcase_33 AC 30 ms
7,936 KB
testcase_34 AC 30 ms
6,912 KB
testcase_35 AC 31 ms
9,088 KB
testcase_36 AC 31 ms
7,936 KB
testcase_37 AC 18 ms
6,676 KB
testcase_38 AC 31 ms
6,912 KB
testcase_39 AC 26 ms
6,676 KB
testcase_40 AC 35 ms
9,088 KB
testcase_41 AC 37 ms
9,600 KB
testcase_42 AC 18 ms
6,676 KB
testcase_43 AC 27 ms
6,676 KB
testcase_44 AC 14 ms
6,676 KB
testcase_45 AC 8 ms
6,676 KB
testcase_46 AC 30 ms
6,676 KB
testcase_47 AC 21 ms
6,676 KB
testcase_48 AC 17 ms
6,676 KB
testcase_49 AC 9 ms
6,676 KB
testcase_50 AC 23 ms
6,676 KB
testcase_51 AC 1 ms
6,676 KB
testcase_52 AC 20 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::vec;

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, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn main() {
    input! {
        n: usize,
        m: usize,
        edges: [(usize, usize); m],
    }

    let graph = {
        let mut graph = vec![vec![]; n];
        for &(a, b) in &edges {
            let a = a - 1;
            let b = b - 1;
            graph[a].push(b);
        }
        graph
    };

    let inf = 998244353;
    let get_dist = |start: usize| -> Vec<usize> {
        let mut dist = vec![inf; n];
        dist[start] = 0;
        let mut queue = std::collections::VecDeque::new();
        queue.push_back(start);
        while let Some(v) = queue.pop_front() {
            let d = dist[v];
            for &e in &graph[v] {
                if dist[e] <= d + 1 {
                    continue;
                }
                dist[e] = d + 1;
                queue.push_back(e);
            }
        }
        dist
    };

    let dist_st = get_dist(0);
    let dist_a = get_dist(n - 2);
    let dist_b = get_dist(n - 1);

    let st_to_a = dist_st[n - 2];
    let st_to_b = dist_st[n - 1];
    let a_to_b = dist_a[n - 1];
    let a_to_st = dist_a[0];
    let b_to_a = dist_b[n - 2];
    let b_to_st = dist_b[0];

    let ans = {
        let ans = (st_to_a + a_to_b + b_to_st).min(st_to_b + b_to_a + a_to_st);
        let mut ans = ans as i64;
        if ans >= inf as i64 {
            ans = -1;
        }
        ans
    };

    println!("{}", ans);
}
0