結果

問題 No.1283 Extra Fee
ユーザー AEn
提出日時 2022-08-31 14:20:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,879 ms / 2,000 ms
コード長 1,183 bytes
コンパイル時間 541 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 108,944 KB
最終ジャッジ日時 2024-11-08 06:12:53
合計ジャッジ時間 23,051 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from heapq import heappush,heappop

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

dis1 = [[float('inf') for _ in range(N)] for _ in range(N)]
dis2 = [[float('inf') for _ in range(N)] for _ in range(N)]
dis1[0][0] = 0
dis2[0][0] = 0
p = [(0,0,0,0)]
xx = [0,1,0,-1]
yy = [1,0,-1,0]
while p:
    c, x, y, f = heappop(p)
    if f==0 and dis1[x][y]<c:
        continue
    if f==1 and dis2[x][y]<c:
        continue
    for px, py in zip(xx, yy):
        cx = x+px
        cy = y+py
        if 0<=cx<N and 0<=cy<N:
            cost1 = c+cost[cx][cy]+1
            cost2 = c+1
            if f:
                if dis2[cx][cy]>cost1:
                    dis2[cx][cy] = cost1
                    heappush(p, (cost1, cx, cy, f))
            else:
                if dis1[cx][cy]>cost1:
                    dis1[cx][cy] = cost1
                    heappush(p, (cost1, cx, cy, f))
                if dis2[cx][cy]>cost2:
                    dis2[cx][cy] = cost2
                    heappush(p, (cost2, cx, cy, f+1))

print(min(dis1[-1][-1],dis2[-1][-1]))
0