結果
問題 | No.2328 Build Walls |
ユーザー |
![]() |
提出日時 | 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 |
ソースコード
from collections import dequeimport bisectimport heapqh,w = map(int, input().split())h-=2a = []M = 10**18for _ in range(h):aa = list(map(int, input().split()))for i in range(w):if(aa[i]==-1):aa[i] = Ma.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):continuefor dy in range(-1,2):yy = y+dyif(yy<0 or h<=yy):continuefor dx in range(-1,2):xx = x+dxif(xx<0 or w<=xx):continuew_new = ww+a[yy][xx]if(weight[yy][xx] > w_new):weight[yy][xx] = w_newheapq.heappush(hh, (w_new, xx, yy))ans = Mfor i in range(h):ans = min(ans, weight[i][-1])if(ans >= M):print(-1)else:print(ans)