結果

問題 No.2328 Build Walls
コンテスト
ユーザー urectanc
提出日時 2026-05-04 18:11:27
言語 Rust
(1.94.0 + proconio + num + itertools)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
AC  
実行時間 86 ms / 3,000 ms
コード長 1,144 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 10,147 ms
コンパイル使用メモリ 189,120 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2026-05-04 18:11:46
合計ジャッジ時間 14,671 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

use itertools::iproduct;
use proconio::input;

fn main() {
    input! {
        h: usize, w: usize,
        a: [[i32; w]; h - 2],
    }
    let h = h - 2;

    let mut heap = BinaryHeap::new();
    let mut dist = vec![vec![u32::MAX; w]; h];
    for i in 0..h {
        let d = a[i][0] as u32;
        dist[i][0] = d;
        heap.push((Reverse(d), i, 0));
    }

    while let Some((Reverse(d), i, j)) = heap.pop() {
        if d != dist[i][j] {
            continue;
        }

        if j == w - 1 {
            println!("{d}");
            return;
        }

        for (di, dj) in iproduct!(-1..=1, -1..=1) {
            if (di, dj) == (0, 0) {
                continue;
            }
            let ni = i.wrapping_add_signed(di);
            let nj = j.wrapping_add_signed(dj);
            if ni >= h || nj >= w {
                continue;
            }
            let nd = d.saturating_add(a[ni][nj] as u32);
            if dist[ni][nj] > nd {
                dist[ni][nj] = nd;
                heap.push((Reverse(nd), ni, nj));
            }
        }
    }

    println!("-1");
}
0