結果

問題 No.2565 はじめてのおつかい
ユーザー あさくちあさくち
提出日時 2023-12-02 16:25:15
言語 Rust
(1.77.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 2,433 bytes
コンパイル時間 1,478 ms
コンパイル使用メモリ 196,032 KB
実行使用メモリ 13,908 KB
最終ジャッジ日時 2023-12-02 16:25:20
合計ジャッジ時間 4,726 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 55 ms
13,908 KB
testcase_04 AC 39 ms
13,908 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 43 ms
8,460 KB
testcase_07 AC 22 ms
6,676 KB
testcase_08 AC 20 ms
6,676 KB
testcase_09 AC 33 ms
6,728 KB
testcase_10 AC 24 ms
7,396 KB
testcase_11 AC 56 ms
13,752 KB
testcase_12 AC 14 ms
6,676 KB
testcase_13 AC 23 ms
6,676 KB
testcase_14 AC 47 ms
9,120 KB
testcase_15 AC 53 ms
9,072 KB
testcase_16 AC 23 ms
7,680 KB
testcase_17 AC 5 ms
6,676 KB
testcase_18 AC 7 ms
6,676 KB
testcase_19 AC 41 ms
10,168 KB
testcase_20 AC 38 ms
9,504 KB
testcase_21 AC 27 ms
7,988 KB
testcase_22 AC 12 ms
6,676 KB
testcase_23 AC 22 ms
6,676 KB
testcase_24 AC 3 ms
6,676 KB
testcase_25 AC 39 ms
9,480 KB
testcase_26 AC 21 ms
6,676 KB
testcase_27 AC 21 ms
6,676 KB
testcase_28 AC 35 ms
8,344 KB
testcase_29 AC 51 ms
10,972 KB
testcase_30 AC 21 ms
6,784 KB
testcase_31 AC 40 ms
9,916 KB
testcase_32 AC 13 ms
6,676 KB
testcase_33 AC 22 ms
6,676 KB
testcase_34 AC 43 ms
9,508 KB
testcase_35 AC 23 ms
7,296 KB
testcase_36 AC 34 ms
9,984 KB
testcase_37 AC 23 ms
6,676 KB
testcase_38 AC 23 ms
6,676 KB
testcase_39 AC 44 ms
8,904 KB
testcase_40 AC 23 ms
7,424 KB
testcase_41 AC 38 ms
9,536 KB
testcase_42 AC 25 ms
6,676 KB
testcase_43 AC 19 ms
6,676 KB
testcase_44 AC 18 ms
6,676 KB
testcase_45 AC 6 ms
6,676 KB
testcase_46 AC 43 ms
9,100 KB
testcase_47 AC 28 ms
6,676 KB
testcase_48 AC 31 ms
7,680 KB
testcase_49 AC 14 ms
6,676 KB
testcase_50 AC 37 ms
6,676 KB
testcase_51 AC 1 ms
6,676 KB
testcase_52 AC 26 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::{HashSet, VecDeque};

fn main() {
    let (n, m) = input_tuple();
    let u_v = input_tuple_vec::<usize>(m);

    let mut list = vec![Vec::new(); n];

    for &(u, v) in &u_v {
        list[u - 1].push(v - 1);
    }

    let start = (0, false, false);

    // ----------------------------------

    // let mut visited = vec![false; n];
    let mut visited = HashSet::new();

    let mut queue = VecDeque::new();
    queue.push_back((start, 0));
    visited.insert(start);

    // let mut steps = vec![usize::MAX; n];
    // steps[start] = 0;

    while let Some(((current, visit_n, visit_n_1), step)) = queue.pop_front() {
        if current == 0 && visit_n && visit_n_1 {
            println!("{}", step);
            return;
        }

        let mut next_visit_n = visit_n;
        let mut next_visit_n_1 = visit_n_1;

        if current == n - 1 {
            next_visit_n = true;
        }

        if current == n - 2 {
            next_visit_n_1 = true;
        }

        for &next in list[current].iter() {
            if visited.contains(&(next, next_visit_n, next_visit_n_1)) {
                continue;
            }

            // 次へ
            visited.insert((next, next_visit_n, next_visit_n_1));
            queue.push_back(((next, next_visit_n, next_visit_n_1), step + 1));
        }
    }

    println!("-1");
}

fn input_tuple<T>() -> (T, T)
where
    T: std::str::FromStr,
    <T as std::str::FromStr>::Err: std::fmt::Debug,
{
    let stdin = std::io::stdin();

    let mut buf = String::new();
    stdin.read_line(&mut buf).unwrap();
    buf = buf.trim_end().to_owned();

    let mut iter = buf.split_whitespace();

    let n = iter.next().unwrap().parse().unwrap();
    let m = iter.next().unwrap().parse().unwrap();

    (n, m)
}

fn input_tuple_vec<T>(n: usize) -> Vec<(T, T)>
where
    T: std::str::FromStr,
    <T as std::str::FromStr>::Err: std::fmt::Debug,
{
    // タプルのベクタ

    let stdin = std::io::stdin();

    let mut s_t_d = Vec::with_capacity(n);

    for _ in 0..n {
        let mut buf = String::new();
        stdin.read_line(&mut buf).unwrap();
        buf = buf.trim_end().to_owned();

        let mut iter = buf.split_whitespace();

        let s = iter.next().unwrap().parse().unwrap();
        let t = iter.next().unwrap().parse().unwrap();
        // let d = iter.next().unwrap().parse().unwrap();

        s_t_d.push((s, t));
    }

    s_t_d
}
0