結果

問題 No.1283 Extra Fee
ユーザー phsplsphspls
提出日時 2022-11-14 23:17:44
言語 Rust
(1.77.0)
結果
AC  
実行時間 966 ms / 2,000 ms
コード長 1,816 bytes
コンパイル時間 891 ms
コンパイル使用メモリ 149,752 KB
実行使用メモリ 8,908 KB
最終ジャッジ日時 2023-10-14 06:20:20
合計ジャッジ時間 9,762 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,356 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 18 ms
4,348 KB
testcase_12 AC 48 ms
4,352 KB
testcase_13 AC 9 ms
4,348 KB
testcase_14 AC 55 ms
4,348 KB
testcase_15 AC 106 ms
4,352 KB
testcase_16 AC 14 ms
4,348 KB
testcase_17 AC 202 ms
7,664 KB
testcase_18 AC 966 ms
8,908 KB
testcase_19 AC 634 ms
8,488 KB
testcase_20 AC 622 ms
8,120 KB
testcase_21 AC 558 ms
8,236 KB
testcase_22 AC 529 ms
7,612 KB
testcase_23 AC 78 ms
7,932 KB
testcase_24 AC 88 ms
7,968 KB
testcase_25 AC 607 ms
8,636 KB
testcase_26 AC 687 ms
8,592 KB
testcase_27 AC 620 ms
8,668 KB
testcase_28 AC 680 ms
8,648 KB
testcase_29 AC 222 ms
8,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::VecDeque;

const INF: usize = 1usize << 60;
const DX: [isize; 4] = [-1, 1, 0, 0];
const DY: [isize; 4] = [0, 0, -1, 1];

fn main() {
    let mut nm = String::new();
    std::io::stdin().read_line(&mut nm).ok();
    let nm: Vec<usize> = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nm[0];
    let m = nm[1];
    let mut costs = vec![vec![0usize; n]; n];
    for _ in 0..m {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let h = temp[0]-1;
        let w = temp[1]-1;
        let c = temp[2];
        costs[h][w] = c;
    }

    let mut grid = vec![vec![vec![INF; n]; n]; 2];
    grid[0][0][0] = 0;
    let mut deque = VecDeque::new();
    deque.push_back((0, 0, 0));
    while let Some((x, y, layer)) = deque.pop_front() {
        for dir in 0..4 {
            let nx = x as isize + DX[dir];
            let ny = y as isize + DY[dir];
            if nx >= 0 && ny >=  0 && nx < n as isize && ny < n as isize {
                let nx = nx as usize;
                let ny = ny as usize;
                if grid[layer][nx][ny] > grid[layer][x][y] + 1 + costs[nx][ny] {
                    grid[layer][nx][ny] = grid[layer][x][y] + 1 + costs[nx][ny];
                    deque.push_back((nx, ny, layer));
                }
                if costs[nx][ny] > 0 && layer == 0 {
                    if grid[layer+1][nx][ny] > grid[layer][x][y] + 1 {
                        grid[layer+1][nx][ny] = grid[layer][x][y] + 1;
                        deque.push_back((nx, ny, layer+1));
                    }
                }
            }
        }
    }
    println!("{}", grid[0][n-1][n-1].min(grid[1][n-1][n-1]));
}
0