結果

問題 No.2328 Build Walls
ユーザー InTheBloomInTheBloom
提出日時 2023-12-15 23:42:44
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 311 ms / 3,000 ms
コード長 1,509 bytes
コンパイル時間 2,586 ms
コンパイル使用メモリ 171,500 KB
実行使用メモリ 14,876 KB
最終ジャッジ日時 2023-12-15 23:42:53
合計ジャッジ時間 7,871 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 41 ms
13,160 KB
testcase_14 AC 83 ms
9,544 KB
testcase_15 AC 67 ms
9,544 KB
testcase_16 AC 12 ms
6,676 KB
testcase_17 AC 65 ms
9,672 KB
testcase_18 AC 3 ms
6,676 KB
testcase_19 AC 7 ms
6,676 KB
testcase_20 AC 7 ms
6,676 KB
testcase_21 AC 32 ms
11,336 KB
testcase_22 AC 130 ms
13,040 KB
testcase_23 AC 262 ms
14,876 KB
testcase_24 AC 244 ms
14,876 KB
testcase_25 AC 261 ms
14,876 KB
testcase_26 AC 199 ms
14,876 KB
testcase_27 AC 233 ms
14,876 KB
testcase_28 AC 59 ms
14,876 KB
testcase_29 AC 263 ms
14,876 KB
testcase_30 AC 71 ms
14,876 KB
testcase_31 AC 68 ms
14,876 KB
testcase_32 AC 228 ms
14,876 KB
testcase_33 AC 311 ms
14,876 KB
testcase_34 AC 75 ms
14,876 KB
testcase_35 AC 278 ms
14,876 KB
testcase_36 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int H, W; readln.read(H, W);
    int[][] A = new int[][](H-2, 0);
    foreach (i; 0..H-2) A[i] = readln.split.to!(int[]);

    solve(H, W, A);
}

void solve (int H, int W, int[][] A) {
    /* dijkstraを回す */

    int[][] cost = new int[][](H-2, W);
    BinaryHeap!(Tuple!(int, int, int)[], "b[2] < a[2]") PQ = [];

    foreach (i; 0..H-2) if (A[i][0] != -1) {
        PQ.insert(tuple(i, 0, A[i][0]));
    }
    foreach (c; cost) c[] = int.max;

    const int[] dxy = [-1, 0, 1];
    bool isInGrid (int y, int x) {
        return 0 <= y && y < H-2 && 0 <= x && x < W;
    }

    while (!PQ.empty) {
        auto head = PQ.front; PQ.removeFront;
        if (cost[head[0]][head[1]] < head[2]) continue;
        cost[head[0]][head[1]] = head[2];

        foreach (dy; dxy) foreach (dx; dxy) if (dy != 0 || dx != 0) {
            int y = head[0] + dy, x = head[1] + dx;
            if (!isInGrid(y, x)) continue;
            if (A[y][x] == -1) continue;
            int NewCost = cost[head[0]][head[1]] + A[y][x];
            if (cost[y][x] <= NewCost) continue;
            cost[y][x] = NewCost;
            PQ.insert(tuple(y, x, NewCost));
        }
    }

    int ans = int.max;
    foreach (i; 0..H-2) ans = min(ans, cost[i][W-1]);

    if (ans < int.max) {
        writeln(ans);
    }
    else {
        writeln(-1);
    }
}

void read (T...) (string S, ref T args) {
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}
0