結果

問題 No.2328 Build Walls
ユーザー pあり
提出日時 2023-05-28 14:50:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,231 ms / 3,000 ms
コード長 930 bytes
コンパイル時間 415 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 90,924 KB
最終ジャッジ日時 2024-12-27 03:08:45
合計ジャッジ時間 14,256 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import bisect
import heapq

h,w = map(int, input().split())
h-=2

a = []
M = 10**18

for _ in range(h):
  aa = list(map(int, input().split()))
  for i in range(w):
    if(aa[i]==-1):
      aa[i] = M
  a.append(aa)
   
    
hh = []
weight = [[M for _ in range(w)] for _ in range(h)]
for i in range(h):
  hh.append((a[i][0], 0, i))
  weight[i][0] =  a[i][0]
  
heapq.heapify(hh)



while(hh):
  
  ww, x, y = heapq.heappop(hh)
  
  if(weight[y][x] < ww):
    continue
    
  for dy in  range(-1,2):
    yy = y+dy
    if(yy<0 or h<=yy):
      continue
    for dx in range(-1,2):
      xx = x+dx
      if(xx<0 or w<=xx):
        continue
        
      w_new = ww+a[yy][xx]
      if(weight[yy][xx] > w_new):
        weight[yy][xx] = w_new
        heapq.heappush(hh, (w_new, xx, yy))
        
ans = M
for i in range(h):
  ans = min(ans, weight[i][-1])
  
if(ans >= M):
  print(-1)
else:
  print(ans)
  


0