結果

問題 No.1283 Extra Fee
ユーザー H3PO4H3PO4
提出日時 2022-02-19 10:13:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 981 ms / 2,000 ms
コード長 1,303 bytes
コンパイル時間 179 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 219,840 KB
最終ジャッジ日時 2024-04-28 00:49:16
合計ジャッジ時間 14,744 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,864 KB
testcase_01 AC 35 ms
53,120 KB
testcase_02 AC 38 ms
52,864 KB
testcase_03 AC 39 ms
52,480 KB
testcase_04 AC 34 ms
52,992 KB
testcase_05 AC 34 ms
52,864 KB
testcase_06 AC 41 ms
59,008 KB
testcase_07 AC 36 ms
53,120 KB
testcase_08 AC 42 ms
60,160 KB
testcase_09 AC 39 ms
53,760 KB
testcase_10 AC 38 ms
53,248 KB
testcase_11 AC 169 ms
83,288 KB
testcase_12 AC 188 ms
85,248 KB
testcase_13 AC 153 ms
82,560 KB
testcase_14 AC 283 ms
102,016 KB
testcase_15 AC 344 ms
116,932 KB
testcase_16 AC 191 ms
86,112 KB
testcase_17 AC 884 ms
191,152 KB
testcase_18 AC 905 ms
207,268 KB
testcase_19 AC 909 ms
214,732 KB
testcase_20 AC 845 ms
205,964 KB
testcase_21 AC 860 ms
208,508 KB
testcase_22 AC 829 ms
194,164 KB
testcase_23 AC 767 ms
217,120 KB
testcase_24 AC 768 ms
216,864 KB
testcase_25 AC 966 ms
219,320 KB
testcase_26 AC 918 ms
219,136 KB
testcase_27 AC 960 ms
219,840 KB
testcase_28 AC 981 ms
219,320 KB
testcase_29 AC 948 ms
201,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from heapq import heappush, heappop

input = sys.stdin.buffer.readline

INF = 10 ** 18


def dijkstra(N, G, s):
    dist = [INF] * N
    que = [(0, s)]
    dist[s] = 0
    while que:
        c, v = heappop(que)
        if dist[v] < c:
            continue
        for t, cost in G[v]:
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                heappush(que, (dist[t], t))
    return dist


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

grid2int = lambda h, w, i: h * N + w + i * N * N

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

size = 2 * N * N
G = [[] for _ in range(size)]

for h in range(N):
    for w in range(N):
        toll = toll_table[h][w]
        for dh, dw in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            hdh = h + dh
            wdw = w + dw
            if not (0 <= hdh < N and 0 <= wdw < N):
                continue
            G[grid2int(h, w, 0)].append((grid2int(hdh, wdw, 0), toll))
            G[grid2int(h, w, 1)].append((grid2int(hdh, wdw, 1), toll))
            if toll > 1:
                G[grid2int(h, w, 0)].append((grid2int(hdh, wdw, 1), 1))

dist = dijkstra(size, G, grid2int(0, 0, 0))
print(dist[grid2int(N - 1, N - 1, 1)])
0