結果

問題 No.2328 Build Walls
ユーザー lloyzlloyz
提出日時 2024-01-19 09:06:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,212 ms / 3,000 ms
コード長 965 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 89,440 KB
最終ジャッジ日時 2024-01-19 09:06:15
合計ジャッジ時間 13,798 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 37 ms
53,460 KB
testcase_03 AC 52 ms
53,460 KB
testcase_04 AC 39 ms
53,460 KB
testcase_05 AC 44 ms
53,460 KB
testcase_06 AC 42 ms
53,460 KB
testcase_07 AC 39 ms
53,460 KB
testcase_08 AC 39 ms
53,460 KB
testcase_09 AC 39 ms
53,460 KB
testcase_10 AC 38 ms
53,460 KB
testcase_11 AC 38 ms
53,460 KB
testcase_12 AC 38 ms
53,460 KB
testcase_13 AC 137 ms
81,784 KB
testcase_14 AC 385 ms
80,120 KB
testcase_15 AC 291 ms
79,364 KB
testcase_16 AC 120 ms
76,936 KB
testcase_17 AC 273 ms
79,992 KB
testcase_18 AC 89 ms
76,284 KB
testcase_19 AC 51 ms
67,832 KB
testcase_20 AC 107 ms
76,372 KB
testcase_21 AC 82 ms
76,792 KB
testcase_22 AC 598 ms
82,780 KB
testcase_23 AC 906 ms
89,440 KB
testcase_24 AC 853 ms
89,004 KB
testcase_25 AC 900 ms
89,204 KB
testcase_26 AC 630 ms
87,948 KB
testcase_27 AC 802 ms
88,800 KB
testcase_28 AC 104 ms
79,992 KB
testcase_29 AC 927 ms
89,260 KB
testcase_30 AC 170 ms
87,160 KB
testcase_31 AC 155 ms
86,904 KB
testcase_32 AC 804 ms
88,688 KB
testcase_33 AC 1,212 ms
89,272 KB
testcase_34 AC 181 ms
87,544 KB
testcase_35 AC 1,009 ms
88,576 KB
testcase_36 AC 39 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush

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

H = []
INF = 10**18
DP = [[INF for _ in range(w)] for _ in range(h)]
for i in range(h):
    if A[i][0] != -1:
        DP[i][0] = A[i][0]
        heappush(H, (DP[i][0], i, 0))
Directions = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]
while H:
    cc, ci, cj = heappop(H)
    if cc > DP[ci][cj]:
        continue
    for di, dj in Directions:
        ni, nj = ci + di, cj + dj
        if 0 <= ni < h and 0 <= nj < w:
            if A[ni][nj] == -1:
                continue
            nc = cc + A[ni][nj]
            if nc >= DP[ni][nj]:
                continue
            DP[ni][nj] = nc
            heappush(H, (nc, ni, nj))
ans = INF
for i in range(h):
    ans = min(ans, DP[i][-1])
if ans == INF:
    ans = -1
print(ans)
0