結果

問題 No.2328 Build Walls
ユーザー googol_S0
提出日時 2023-05-28 14:03:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,140 ms / 3,000 ms
コード長 755 bytes
コンパイル時間 1,433 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 90,084 KB
最終ジャッジ日時 2024-12-26 23:12:41
合計ジャッジ時間 12,917 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

H,W=map(int,input().split())
H-=2
A=[list(map(int,input().split())) for i in range(H)]
INF=10**10
for i in range(H):
    for j in range(W):
        if A[i][j]==-1:
            A[i][j]=INF
from heapq import *
D=[[INF]*W for i in range(H)]
Q=[]
heapify(Q)
for i in range(H):
    heappush(Q,(A[i][0],i,0))
    D[i][0]=A[i][0]
while len(Q):
    v=heappop(Q)
    x,y=v[1],v[2]
    if v[0]>D[x][y]:
        continue
    for i in range(-1,2):
        for j in range(-1,2):
            p,q=x+i,y+j
            if 0<=p and 0<=q and p<H and q<W:
                if D[x][y]+A[p][q]<D[p][q]:
                    D[p][q]=D[x][y]+A[p][q]
                    heappush(Q,(D[p][q],p,q))
ANS=min([D[i][-1] for i in range(H)])
if ANS<INF:
    print(ANS)
else:
    print(-1)
0