結果

問題 No.1283 Extra Fee
ユーザー c-yanc-yan
提出日時 2020-11-12 10:49:07
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,997 bytes
コンパイル時間 513 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 118,936 KB
最終ジャッジ日時 2024-07-22 19:07:09
合計ジャッジ時間 31,474 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,376 KB
testcase_01 AC 42 ms
53,376 KB
testcase_02 AC 42 ms
53,504 KB
testcase_03 AC 40 ms
53,376 KB
testcase_04 AC 42 ms
53,120 KB
testcase_05 AC 44 ms
53,632 KB
testcase_06 AC 49 ms
53,760 KB
testcase_07 AC 45 ms
53,248 KB
testcase_08 AC 44 ms
54,144 KB
testcase_09 AC 43 ms
54,028 KB
testcase_10 AC 44 ms
54,128 KB
testcase_11 AC 447 ms
83,848 KB
testcase_12 AC 528 ms
84,220 KB
testcase_13 AC 320 ms
80,772 KB
testcase_14 AC 664 ms
84,376 KB
testcase_15 AC 895 ms
88,780 KB
testcase_16 AC 399 ms
81,540 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 AC 1,969 ms
102,924 KB
testcase_20 AC 1,864 ms
99,760 KB
testcase_21 AC 1,875 ms
99,688 KB
testcase_22 AC 1,696 ms
98,736 KB
testcase_23 AC 1,051 ms
87,044 KB
testcase_24 AC 1,054 ms
87,328 KB
testcase_25 AC 1,992 ms
102,448 KB
testcase_26 TLE -
testcase_27 AC 1,934 ms
100,912 KB
testcase_28 TLE -
testcase_29 TLE -
権限があれば一括ダウンロードができます

ソースコード

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
        if h != 0:
            nh = h - 1
            nw = w
            if dp0[nh][nw] > dp0[h][w] + 1 + c[nh][nw]:
                dp0[nh][nw] = dp0[h][w] + 1 + c[nh][nw]
                heappush(q, (dp0[nh][nw], nh, nw, 0))
            if c[nh][nw] > 0 and dp1[nh][nw] > dp0[h][w] + 1:
                dp1[nh][nw] = dp0[h][w] + 1
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if h != N - 1:
            nh = h + 1
            nw = w
            if dp0[nh][nw] > dp0[h][w] + 1 + c[nh][nw]:
                dp0[nh][nw] = dp0[h][w] + 1 + c[nh][nw]
                heappush(q, (dp0[nh][nw], nh, nw, 0))
            if c[nh][nw] > 0 and dp1[nh][nw] > dp0[h][w] + 1:
                dp1[nh][nw] = dp0[h][w] + 1
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if w != 0:
            nh = h
            nw = w - 1
            if dp0[nh][nw] > dp0[h][w] + 1 + c[nh][nw]:
                dp0[nh][nw] = dp0[h][w] + 1 + c[nh][nw]
                heappush(q, (dp0[nh][nw], nh, nw, 0))
            if c[nh][nw] > 0 and dp1[nh][nw] > dp0[h][w] + 1:
                dp1[nh][nw] = dp0[h][w] + 1
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if w != N - 1:
            nh = h
            nw = w + 1
            if dp0[nh][nw] > dp0[h][w] + 1 + c[nh][nw]:
                dp0[nh][nw] = dp0[h][w] + 1 + c[nh][nw]
                heappush(q, (dp0[nh][nw], nh, nw, 0))
            if c[nh][nw] > 0 and dp1[nh][nw] > dp0[h][w] + 1:
                dp1[nh][nw] = dp0[h][w] + 1
                heappush(q, (dp1[nh][nw], nh, nw, 1))
    elif t == 1:
        if dp1[h][w] < cc:
            continue
        if h != 0:
            nh = h - 1
            nw = w
            if dp1[nh][nw] > dp1[h][w] + 1 + c[nh][nw]:
                dp1[nh][nw] = dp1[h][w] + 1 + c[nh][nw]
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if h != N - 1:
            nh = h + 1
            nw = w
            if dp1[nh][nw] > dp1[h][w] + 1 + c[nh][nw]:
                dp1[nh][nw] = dp1[h][w] + 1 + c[nh][nw]
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if w != 0:
            nh = h
            nw = w - 1
            if dp1[nh][nw] > dp1[h][w] + 1 + c[nh][nw]:
                dp1[nh][nw] = dp1[h][w] + 1 + c[nh][nw]
                heappush(q, (dp1[nh][nw], nh, nw, 1))
        if w != N - 1:
            nh = h
            nw = w + 1
            if dp1[nh][nw] > dp1[h][w] + 1 + c[nh][nw]:
                dp1[nh][nw] = dp1[h][w] + 1 + c[nh][nw]
                heappush(q, (dp1[nh][nw], nh, nw, 1))
print(min(dp0[N - 1][N - 1], dp1[N - 1][N - 1]))
0