結果

問題 No.92 逃走経路
コンテスト
ユーザー elphe
提出日時 2026-02-12 11:52:39
言語 Rust
(1.93.0 + proconio + num + itertools)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,195 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 14,604 ms
コンパイル使用メモリ 203,576 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-02-12 11:52:55
合計ジャッジ時間 3,916 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

fn main() {
    let stdin = std::io::read_to_string(std::io::stdin().lock()).unwrap();
    let mut stdin = stdin.split_ascii_whitespace();

    let n: usize = stdin.next().unwrap().parse().unwrap();
    let m: usize = stdin.next().unwrap().parse().unwrap();
    let k: usize = stdin.next().unwrap().parse().unwrap();
    let roads: Vec<(u16, u16, u32)> = (0..m)
        .map(|_| {
            (
                stdin.next().unwrap().parse().unwrap(),
                stdin.next().unwrap().parse().unwrap(),
                stdin.next().unwrap().parse().unwrap(),
            )
        })
        .collect();
    let d: Vec<u32> = (0..k)
        .map(|_| stdin.next().unwrap().parse().unwrap())
        .collect();

    use std::io::Write;
    std::io::stdout()
        .lock()
        .write_all(output(solve(n, roads, d)).as_bytes())
        .unwrap();
}

fn prepare(n: usize, roads: Vec<(u16, u16, u32)>) -> Vec<Vec<(u32, u32)>> {
    let mut next_of = vec![Vec::new(); n + 1];
    roads.into_iter().for_each(|(a, b, c)| {
        next_of[a as usize].push((c, b as u32));
        next_of[b as usize].push((c, a as u32));
    });
    next_of
}

fn solve(n: usize, roads: Vec<(u16, u16, u32)>, d: Vec<u32>) -> Vec<u8> {
    let next_of = prepare(n, roads);
    let mut dp_cur = vec![false; n + 1];
    let mut dp_next = vec![true; n + 1];
    d.into_iter().for_each(|d| {
        std::mem::swap(&mut dp_cur, &mut dp_next);
        dp_next.fill(false);
        dp_cur
            .iter()
            .enumerate()
            .filter(|&(_, &b)| b)
            .for_each(|(i, _)| {
                next_of[i]
                    .iter()
                    .filter(|&&(cost, _)| cost == d)
                    .for_each(|&(_, next)| {
                        dp_next[next as usize] = true;
                    })
            })
    });
    dp_next
        .into_iter()
        .enumerate()
        .filter(|&(_, b)| b)
        .map(|(i, _)| i as u8)
        .collect()
}

fn output(ans: Vec<u8>) -> String {
    ans.len().to_string()
        + "\n"
        + &ans
            .into_iter()
            .map(|x| x.to_string())
            .collect::<Vec<_>>()
            .join(" ")
        + "\n"
}
0