結果

問題 No.1283 Extra Fee
ユーザー vwxyz
提出日時 2023-12-16 07:34:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,728 ms / 2,000 ms
コード長 1,138 bytes
コンパイル時間 495 ms
コンパイル使用メモリ 82,504 KB
実行使用メモリ 103,200 KB
最終ジャッジ日時 2024-09-27 07:29:50
合計ジャッジ時間 18,885 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
import heapq

N,M=map(int,readline().split())
C=[[0]*N for h in range(N)]
for m in range(M):
    h,w,c=map(int,readline().split())
    h-=1;w-=1
    C[h][w]=c
inf=1<<60
dist0=[[inf]*N for h in range(N)]
dist1=[[inf]*N for h in range(N)]
dist0[0][0]=0
queue=[(0,0,0,0)]
while queue:
    dx,bx,hx,wx=heapq.heappop(queue)
    if bx==0:
        if dist0[hx][wx]<dx:
            continue
    else:
        if dist1[hx][wx]<dx:
            continue
    for dh,dw in ((0,1),(1,0),(0,-1),(-1,0)):
        hy=hx+dh
        wy=wx+dw
        if 0<=hy<N and 0<=wy<N:
            if bx==0:
                if dist0[hy][wy]>dx+C[hy][wy]+1:
                    dist0[hy][wy]=dx+C[hy][wy]+1
                    heapq.heappush(queue,(dist0[hy][wy],bx,hy,wy))
                if dist1[hy][wy]>dx+1:
                    dist1[hy][wy]=dx+1
                    heapq.heappush(queue,(dist1[hy][wy],1,hy,wy))
            else:
                if dist1[hy][wy]>dx+C[hy][wy]+1:
                    dist1[hy][wy]=dx+C[hy][wy]+1
                    heapq.heappush(queue,(dist1[hy][wy],bx,hy,wy))
ans=dist1[N-1][N-1]
print(ans)
0