結果

問題 No.1283 Extra Fee
ユーザー H3PO4
提出日時 2022-02-19 10:13:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,085 ms / 2,000 ms
コード長 1,303 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 219,564 KB
最終ジャッジ日時 2024-11-16 08:38:15
合計ジャッジ時間 16,317 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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