結果

問題 No.2328 Build Walls
ユーザー Seed57_cashSeed57_cash
提出日時 2023-05-02 15:58:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,448 ms / 3,000 ms
コード長 1,029 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 82,528 KB
実行使用メモリ 96,916 KB
最終ジャッジ日時 2024-05-01 04:53:29
合計ジャッジ時間 15,405 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,060 KB
testcase_01 AC 38 ms
53,496 KB
testcase_02 AC 41 ms
52,812 KB
testcase_03 AC 36 ms
53,432 KB
testcase_04 AC 36 ms
54,572 KB
testcase_05 AC 35 ms
53,264 KB
testcase_06 AC 35 ms
53,228 KB
testcase_07 AC 37 ms
53,636 KB
testcase_08 AC 36 ms
53,576 KB
testcase_09 AC 36 ms
54,256 KB
testcase_10 AC 37 ms
54,000 KB
testcase_11 AC 36 ms
53,480 KB
testcase_12 AC 40 ms
53,084 KB
testcase_13 AC 160 ms
82,500 KB
testcase_14 AC 617 ms
85,388 KB
testcase_15 AC 363 ms
81,200 KB
testcase_16 AC 159 ms
78,328 KB
testcase_17 AC 324 ms
81,528 KB
testcase_18 AC 110 ms
77,432 KB
testcase_19 AC 46 ms
67,996 KB
testcase_20 AC 158 ms
78,064 KB
testcase_21 AC 78 ms
77,076 KB
testcase_22 AC 962 ms
93,712 KB
testcase_23 AC 982 ms
92,964 KB
testcase_24 AC 820 ms
90,472 KB
testcase_25 AC 1,031 ms
92,032 KB
testcase_26 AC 576 ms
89,048 KB
testcase_27 AC 786 ms
90,580 KB
testcase_28 AC 99 ms
80,744 KB
testcase_29 AC 1,057 ms
91,928 KB
testcase_30 AC 192 ms
88,208 KB
testcase_31 AC 169 ms
88,332 KB
testcase_32 AC 780 ms
90,464 KB
testcase_33 AC 1,448 ms
96,916 KB
testcase_34 AC 233 ms
89,016 KB
testcase_35 AC 1,038 ms
92,128 KB
testcase_36 AC 36 ms
53,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush


h, w = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h - 2)]

# 最小カットするのは面倒なので、左から右にpathを通して切る
distance = [[-1] * w for _ in range(h - 2)]
heap_queue = []

for i in range(h - 2):
    if a[i][0] >= 0:
        heappush(heap_queue, (a[i][0], i * w))

while len(heap_queue):
    d, ij0 = heappop(heap_queue)
    i0, j0 = ij0 // w, ij0 % w
    if distance[i0][j0] == -1:
        distance[i0][j0] = d
    else:
        continue
    
    # 8方向にpath
    for i1 in range(max(0, i0 - 1), min(h - 2, i0 + 2)):
        for j1 in range(max(0, j0 - 1), min(w, j0 + 2)):
            if i1 == i0 and j1 == j0:
                continue
            if a[i1][j1] >= 0 and distance[i1][j1] == -1:
                heappush(heap_queue, (distance[i0][j0] + a[i1][j1], i1* w + j1))

res_list = [distance[i][-1] for i in range(h - 2)]
if max(res_list) == -1:
    print(-1)
else:
    print(min([r for r in res_list if r >= 0]))
0