結果

問題 No.2328 Build Walls
ユーザー rlangevinrlangevin
提出日時 2023-05-30 12:26:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,400 ms / 3,000 ms
コード長 1,640 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 317,856 KB
最終ジャッジ日時 2023-08-28 00:31:04
合計ジャッジ時間 16,110 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,200 KB
testcase_01 AC 75 ms
71,296 KB
testcase_02 AC 75 ms
71,012 KB
testcase_03 AC 76 ms
71,216 KB
testcase_04 AC 74 ms
71,204 KB
testcase_05 AC 78 ms
71,448 KB
testcase_06 AC 77 ms
71,304 KB
testcase_07 AC 76 ms
71,444 KB
testcase_08 AC 75 ms
71,268 KB
testcase_09 AC 76 ms
71,116 KB
testcase_10 AC 76 ms
71,428 KB
testcase_11 AC 75 ms
71,212 KB
testcase_12 AC 76 ms
71,428 KB
testcase_13 AC 301 ms
125,932 KB
testcase_14 AC 463 ms
133,180 KB
testcase_15 AC 354 ms
114,396 KB
testcase_16 AC 178 ms
84,704 KB
testcase_17 AC 349 ms
113,648 KB
testcase_18 AC 144 ms
79,240 KB
testcase_19 AC 110 ms
81,512 KB
testcase_20 AC 173 ms
81,648 KB
testcase_21 AC 172 ms
105,556 KB
testcase_22 AC 636 ms
163,664 KB
testcase_23 AC 931 ms
215,780 KB
testcase_24 AC 902 ms
203,924 KB
testcase_25 AC 935 ms
213,860 KB
testcase_26 AC 723 ms
181,888 KB
testcase_27 AC 883 ms
197,928 KB
testcase_28 AC 218 ms
133,652 KB
testcase_29 AC 953 ms
216,164 KB
testcase_30 AC 387 ms
167,020 KB
testcase_31 AC 373 ms
159,492 KB
testcase_32 AC 841 ms
195,252 KB
testcase_33 AC 1,400 ms
317,856 KB
testcase_34 AC 415 ms
170,804 KB
testcase_35 AC 1,109 ms
255,644 KB
testcase_36 AC 76 ms
71,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0