結果

問題 No.2328 Build Walls
ユーザー ntudantuda
提出日時 2023-06-10 10:43:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 768 ms / 3,000 ms
コード長 770 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 91,120 KB
最終ジャッジ日時 2023-08-30 20:23:44
合計ジャッジ時間 14,568 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,324 KB
testcase_01 AC 80 ms
71,104 KB
testcase_02 AC 79 ms
71,500 KB
testcase_03 AC 78 ms
71,260 KB
testcase_04 AC 79 ms
71,468 KB
testcase_05 AC 77 ms
71,392 KB
testcase_06 AC 77 ms
71,104 KB
testcase_07 AC 76 ms
71,180 KB
testcase_08 AC 78 ms
71,236 KB
testcase_09 AC 78 ms
71,412 KB
testcase_10 AC 79 ms
71,228 KB
testcase_11 AC 78 ms
71,412 KB
testcase_12 AC 78 ms
71,096 KB
testcase_13 AC 188 ms
83,904 KB
testcase_14 AC 348 ms
81,968 KB
testcase_15 AC 300 ms
81,140 KB
testcase_16 AC 174 ms
78,808 KB
testcase_17 AC 282 ms
82,316 KB
testcase_18 AC 135 ms
77,844 KB
testcase_19 AC 92 ms
75,732 KB
testcase_20 AC 159 ms
79,248 KB
testcase_21 AC 121 ms
79,388 KB
testcase_22 AC 455 ms
84,224 KB
testcase_23 AC 685 ms
90,656 KB
testcase_24 AC 656 ms
90,876 KB
testcase_25 AC 655 ms
90,436 KB
testcase_26 AC 504 ms
89,384 KB
testcase_27 AC 597 ms
90,572 KB
testcase_28 AC 141 ms
81,288 KB
testcase_29 AC 662 ms
90,904 KB
testcase_30 AC 211 ms
89,232 KB
testcase_31 AC 194 ms
89,076 KB
testcase_32 AC 580 ms
90,160 KB
testcase_33 AC 768 ms
91,120 KB
testcase_34 AC 256 ms
88,820 KB
testcase_35 AC 719 ms
90,520 KB
testcase_36 AC 78 ms
71,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *

H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H - 2)]
INF = 10 ** 9

D = [[INF] * (H - 2) for _ in range(W)]
q = []
dir = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]

for i in range(H - 2):
    a = A[i][0]
    if a != -1:
        heappush(q, (a, (0, i)))
        D[0][i] = a

while q:
    d, (x, y) = heappop(q)
    if d > D[x][y]:
        continue
    for xd, yd in dir:
        xd += x
        yd += y
        if 0 <= xd < W and 0 <= yd < H - 2 and A[yd][xd] != -1:
            if D[xd][yd] > D[x][y] + A[yd][xd]:
                D[xd][yd] = D[x][y] + A[yd][xd]
                heappush(q, (D[xd][yd], (xd, yd)))

ans = min(D[-1])
if ans == INF:
    print(-1)
else:
    print(ans)
0