結果

問題 No.1283 Extra Fee
ユーザー c-yanc-yan
提出日時 2020-11-12 10:58:55
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,915 bytes
コンパイル時間 753 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 80,128 KB
最終ジャッジ日時 2024-07-22 19:07:16
合計ジャッジ時間 6,215 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush

INF = 10 ** 15

N, M = map(int, input().split())

c = [[0] * N for _ in range(N)]
for _ in range(M):
    h, w, cc = map(int, input().split())
    c[h - 1][w - 1] = cc

dp0 = [[INF] * N for _ in range(N)]
dp1 = [[INF] * N for _ in range(N)]
dp0[0][0] = 0
q = [(0, 0, 0, 0)]
while q:
    cc, h, w, t = heappop(q)
    if t == 0:
        if dp0[h][w] < cc:
            continue
        cc = dp0[h][w]
        if h != 0:
            nh = h - 1
            nw = w
            if dp0[nh][nw] > cc + 1 + dp0[nh][nw]:
                dp0[nh][nw] = cc + 1 + dp0[nh][nw]
                heappush(q, (dp0[nh][nw], nh, nw, 0))
            if dp1[nh][nw] > 0 and dp1[nh][nw] > cc + 1:
                dp1[nh][nw] = cc + 1
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if h != N - 1:
            nh = h + 1
            nw = w
            if dp0[nh][nw] > cc + 1 + dp0[nh][nw]:
                dp0[nh][nw] = cc + 1 + dp0[nh][nw]
                heappush(q, (dp0[nh][nw], nh, nw, 0))
            if dp1[nh][nw] > 0 and dp1[nh][nw] > cc + 1:
                dp1[nh][nw] = cc + 1
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if w != 0:
            nh = h
            nw = w - 1
            if dp0[nh][nw] > cc + 1 + dp0[nh][nw]:
                dp0[nh][nw] = cc + 1 + dp0[nh][nw]
                heappush(q, (dp0[nh][nw], nh, nw, 0))
            if dp1[nh][nw] > 0 and dp1[nh][nw] > cc + 1:
                dp1[nh][nw] = cc + 1
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if w != N - 1:
            nh = h
            nw = w + 1
            if dp0[nh][nw] > cc + 1 + dp0[nh][nw]:
                dp0[nh][nw] = cc + 1 + dp0[nh][nw]
                heappush(q, (dp0[nh][nw], nh, nw, 0))
            if dp1[nh][nw] > 0 and dp1[nh][nw] > cc + 1:
                dp1[nh][nw] = cc + 1
                heappush(q, (dp1[nh][nw], nh, nw, 1))
    elif t == 1:
        if dp1[h][w] < cc:
            continue
        cc = dp1[h][w]
        if h != 0:
            nh = h - 1
            nw = w
            if dp1[nh][nw] > cc + 1 + dp1[nh][nw]:
                dp1[nh][nw] = cc + 1 + dp1[nh][nw]
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if h != N - 1:
            nh = h + 1
            nw = w
            if dp1[nh][nw] > cc + 1 + dp1[nh][nw]:
                dp1[nh][nw] = cc + 1 + dp1[nh][nw]
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if w != 0:
            nh = h
            nw = w - 1
            if dp1[nh][nw] > cc + 1 + dp1[nh][nw]:
                dp1[nh][nw] = cc + 1 + dp1[nh][nw]
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if w != N - 1:
            nh = h
            nw = w + 1
            if dp1[nh][nw] > cc + 1 + dp1[nh][nw]:
                dp1[nh][nw] = cc + 1 + dp1[nh][nw]
                heappush(q, (dp1[nh][nw], nh, nw, 1))
print(min(dp0[N - 1][N - 1], dp1[N - 1][N - 1]))
0