結果

問題 No.2328 Build Walls
ユーザー とりゐ
提出日時 2023-05-28 14:11:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,497 ms / 3,000 ms
コード長 1,086 bytes
コンパイル時間 642 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 95,404 KB
最終ジャッジ日時 2024-12-26 23:52:30
合計ジャッジ時間 17,140 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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)]
from heapq import heappush,heappop

INF=10**18
def dijkstra(start,n):
  dist=[INF]*n
  hq=[(0,start)]
  dist[start]=0
  seen=[False]*n
  while hq:
    w,v=heappop(hq)
    if dist[v]<w:
      continue
    seen[v]=True
    for to,cost in edge[v]:
      if seen[to]==False and dist[v]+cost<dist[to]:
        dist[to]=dist[v]+cost
        heappush(hq,(dist[to],to))
  return dist

inf=1<<60
dist=[[inf]*w for i in range(h)]
seen=[[False]*w for i in range(h)]
hq=[]
for i in range(h):
  if a[i][0]!=-1:
    heappush(hq,(a[i][0],i,0))
    dist[i][0]=a[i][0]

while hq:
  _,x,y=heappop(hq)
  if seen[x][y]:
    continue
  seen[x][y]=True
  for dx in range(-1,2):
    for dy in range(-1,2):
      nx,ny=x+dx,y+dy
      if 0<=nx<h and 0<=ny<w:
        if seen[nx][ny]==False and a[nx][ny]!=-1 and dist[x][y]+a[nx][ny]<dist[nx][ny]:
          dist[nx][ny]=dist[x][y]+a[nx][ny]
          heappush(hq,(dist[nx][ny],nx,ny))

ans=inf
for i in range(h):
  ans=min(ans,dist[i][w-1])
if ans==inf:
  ans=-1
print(ans)
0