結果

問題 No.1283 Extra Fee
ユーザー Kohei
提出日時 2026-11-10 22:13:33
言語 PyPy3
(8.0.1 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,675 ms / 2,000 ms
+ 521µs
コード長 1,273 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 99 ms
コンパイル使用メモリ 64,640 KB
実行使用メモリ 140,308 KB
最終ジャッジ日時 2026-11-10 22:14:02
合計ジャッジ時間 24,860 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

INF = 1e18
from heapq import heappop, heappush
def main():
    N, M = map(int, input().split())
    def check(i, j):
        return 0 <= i < N and 0 <= j < N

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

    kakutei = [[[False] * (2) for _ in range(N)] for _ in range(N)]
    cost = [[[INF] * (2) for _ in range(N)] for _ in range(N)]
    cost[0][0][0] = 0
    que = []
    heappush(que, (0, 0, 0, 0)) # cost_cur, i, j, k
    while que:
        curr_cost, i, j, k = heappop(que)
        if kakutei[i][j][k]:
            continue
        kakutei[i][j][k] = True
        for ii, jj in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
            ni, nj = i+ii, j+jj
            if not check(ni, nj):
                continue
            # 同じ高さ
            if cost[ni][nj][k] > curr_cost + memo[ni][nj] + 1:
                cost[ni][nj][k] = curr_cost + memo[ni][nj] + 1
                heappush(que, (cost[ni][nj][k], ni, nj, k))
            if k == 0 and cost[ni][nj][1] > curr_cost + 1:
                cost[ni][nj][1] = curr_cost + 1
                heappush(que, (cost[ni][nj][1], ni, nj, 1))
    print(min(cost[-1][-1]))
if __name__ == "__main__":
    main()
0