結果

問題 No.2328 Build Walls
ユーザー InTheBloomInTheBloom
提出日時 2023-07-21 13:15:24
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 2,693 bytes
コンパイル時間 6,966 ms
コンパイル使用メモリ 177,748 KB
実行使用メモリ 155,356 KB
最終ジャッジ日時 2023-10-21 14:03:45
合計ジャッジ時間 23,525 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

// 構造体のままダイクストラ(ただし、重みはメンバ変数として管理)を試してみる

struct node {
    int y;
    int x;
    int weight;
}

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

    // グラフ構築
    int[] dxy = [-1, 0, 1];
    node[][][] graph = new node[][][](H-2, W+1, 0);
    foreach (i; 0..H-2) {
        foreach (j; 0..W+1) {

            // 8方向に辺を張る
            foreach (dy; dxy) {
                foreach (dx; dxy) {
                    if (dy == 0 && dx == 0) {
                        continue;
                    }
                    if (H-2 <= i+dy || W+1 <= j+dx || i+dy < 0 || j+dx < 0) {
                        continue;
                    }
                    if (A[i+dy][j+dx] == -1) {
                        continue;
                    }
                    graph[i][j] ~= node(i+dy, j+dx, A[i+dy][j+dx]);
                }
            }
        }
    }

    solve(H, W, A, graph);
}

struct toNode {
    int y;
    int x;
    int totalCost;
}

void solve (int H, int W, int[][] A, node[][][] graph) {
    int[][] comfirmedCost = new int[][](H-2, W+1);
    foreach (ref c; comfirmedCost) {
        c[] = int.max;
    }
    BinaryHeap!(Array!toNode, "a.totalCost > b.totalCost") PQ;

    // 最初の一列はタダで張れるので、最初の一列のどこかを確定頂点にする
    PQ.insert(toNode(0, 0, 0));

    int count = 0;
    int all = 0;
    while (!PQ.empty) {
        all++;
        auto shortest = PQ.front; PQ.removeFront;
        if (comfirmedCost[shortest.y][shortest.x] < shortest.totalCost) {
            count++;
            continue;
        }

        comfirmedCost[shortest.y][shortest.x] = shortest.totalCost;
        // 新しく追加された確定頂点から候補を伸ばす
        foreach (to; graph[shortest.y][shortest.x]) {
            if (to.weight + shortest.totalCost < comfirmedCost[to.y][to.x]) {
                PQ.insert(toNode(to.y, to.x, to.weight + shortest.totalCost));
                comfirmedCost[to.y][to.x] = to.weight + shortest.totalCost;
            }
        }
    }
    writeln("count = ", count);
    writeln("all = ", all);

    // 端に到達できたかチェック
    int ans = int.max;
    foreach (i; 0..H-2) {
        ans = min(ans, comfirmedCost[i][W]);
    }

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

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