結果

問題 No.2328 Build Walls
ユーザー haihamabossuhaihamabossu
提出日時 2023-05-28 15:38:36
言語 Rust
(1.77.0)
結果
AC  
実行時間 153 ms / 3,000 ms
コード長 2,639 bytes
コンパイル時間 5,569 ms
コンパイル使用メモリ 152,828 KB
実行使用メモリ 12,768 KB
最終ジャッジ日時 2023-08-27 12:35:59
合計ジャッジ時間 7,351 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 38 ms
7,748 KB
testcase_14 AC 42 ms
4,928 KB
testcase_15 AC 39 ms
4,844 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 42 ms
5,492 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 32 ms
7,228 KB
testcase_22 AC 63 ms
6,236 KB
testcase_23 AC 151 ms
12,676 KB
testcase_24 AC 148 ms
12,708 KB
testcase_25 AC 153 ms
12,744 KB
testcase_26 AC 128 ms
12,764 KB
testcase_27 AC 143 ms
12,728 KB
testcase_28 AC 61 ms
12,764 KB
testcase_29 AC 153 ms
12,756 KB
testcase_30 AC 69 ms
12,724 KB
testcase_31 AC 68 ms
12,676 KB
testcase_32 AC 141 ms
12,764 KB
testcase_33 AC 147 ms
12,680 KB
testcase_34 AC 71 ms
12,768 KB
testcase_35 AC 150 ms
12,704 KB
testcase_36 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod scanner {

    pub struct Scanner {
        buf: Vec<String>,
    }

    impl Scanner {
        pub fn new() -> Self {
            Self { buf: vec![] }
        }

        pub fn new_from(source: &str) -> Self {
            let source = String::from(source);
            let buf = Self::split(source);
            Self { buf }
        }

        pub fn next<T: std::str::FromStr>(&mut self) -> T {
            loop {
                if let Some(x) = self.buf.pop() {
                    return x.parse().ok().expect("");
                }
                let mut source = String::new();
                std::io::stdin().read_line(&mut source).expect("");
                self.buf = Self::split(source);
            }
        }

        fn split(source: String) -> Vec<String> {
            source
                .split_whitespace()
                .rev()
                .map(String::from)
                .collect::<Vec<_>>()
        }
    }
}

use std::cmp::Reverse;

use crate::scanner::Scanner;

const DIJ: [(usize, usize); 8] = [
    (!0, !0),
    (!0, 0),
    (!0, 1),
    (0, !0),
    (0, 1),
    (1, !0),
    (1, 0),
    (1, 1),
];

fn main() {
    let mut scanner = Scanner::new();
    let t: usize = 1;
    for _ in 0..t {
        solve(&mut scanner);
    }
}

fn solve(scanner: &mut Scanner) {
    let h: usize = scanner.next();
    let w: usize = scanner.next();
    let h = h - 2;
    let mut grid = vec![vec![-1 as isize; w]; h];
    let mut wall = vec![vec![false; w]; h];
    for i in 0..h {
        for j in 0..w {
            grid[i][j] = scanner.next();
            wall[i][j] = grid[i][j] == -1;
        }
    }
    let inf = 1e18 as isize;
    let mut dist = vec![vec![inf; w]; h];
    let mut que = std::collections::BinaryHeap::new();
    for i in 0..h {
        if !wall[i][0] {
            dist[i][0] = grid[i][0];
            que.push(Reverse((grid[i][0], i, 0)));
        }
    }
    while let Some(Reverse((cd, ci, cj))) = que.pop() {
        if dist[ci][cj] < cd {
            continue;
        }
        for &(di, dj) in DIJ.iter() {
            let ni = ci + di;
            let nj = cj + dj;
            if h <= ni || w <= nj || wall[ni][nj] {
                continue;
            }
            let nd = cd + grid[ni][nj];
            if dist[ni][nj] <= nd {
                continue;
            }
            dist[ni][nj] = nd;
            que.push(Reverse((nd, ni, nj)));
        }
    }
    let mut ans = inf;
    for i in 0..h {
        if dist[i][w - 1] < inf {
            ans = ans.min(dist[i][w - 1]);
        }
    }
    if ans == inf {
        ans = -1;
    }
    println!("{}", ans);
}
0