結果
問題 | No.2328 Build Walls |
ユーザー |
![]() |
提出日時 | 2023-05-30 12:26:41 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,459 ms / 3,000 ms |
コード長 | 1,640 bytes |
コンパイル時間 | 374 ms |
コンパイル使用メモリ | 82,240 KB |
実行使用メモリ | 316,600 KB |
最終ジャッジ日時 | 2024-12-28 11:42:06 |
合計ジャッジ時間 | 14,996 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
from heapq import heappush, heappop inf = float('inf') def dijkstra(s, g, N): # ゴールがない場合はg=-1とする。 def cost(v, m): return v * N + m dist = [inf] * N mindist = [inf] * N seen = [False] * N Q = [cost(0, s)] while Q: c, m = divmod(heappop(Q), N) if seen[m]: continue seen[m] = True dist[m] = c if m == g: return dist #------heapをアップデートする。-------- for u, C in G[m]: if seen[u]: continue newdist = dist[m] + C #------------------------------------ if newdist >= mindist[u]: continue mindist[u] = newdist heappush(Q, cost(newdist, u)) return dist H, W = map(int, input().split()) A = [[-1] * (W + 1)] for i in range(H - 2): A.append(list(map(int, input().split())) + [-1]) A.append([-1] * (W + 1)) N = (H + 1) * (W + 1) def f(h, w): return h * (W + 1) + w G = [[] for i in range(N)] for i in range(H): if A[i][0] == -1: continue G[0].append((f(i, 0), A[i][0])) ddx = [1, 1, 1, 0, 0, -1, -1, -1] ddy = [1, 0, -1, 1, -1, 1, 0, -1] for h in range(H): for w in range(W + 1): if A[h][w] == -1: continue for dx, dy in zip(ddx, ddy): x = h + dx y = w + dy if A[x][y] == -1: continue G[f(h, w)].append((f(x, y), A[x][y])) D = dijkstra(0, -1, N) ans = inf for i in range(H): ans = min(ans, D[f(i, W - 1)]) print(ans) if ans != inf else print(-1)