結果
| 問題 | 
                            No.1283 Extra Fee
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-11-07 02:47:34 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,534 ms / 2,000 ms | 
| コード長 | 1,497 bytes | 
| コンパイル時間 | 243 ms | 
| コンパイル使用メモリ | 82,312 KB | 
| 実行使用メモリ | 121,048 KB | 
| 最終ジャッジ日時 | 2024-11-16 06:58:21 | 
| 合計ジャッジ時間 | 20,518 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 30 | 
ソースコード
import heapq
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
fees=[[0]*n for _ in range(n)]
for _ in range(m):
    y,x,cost=map(int,input().split())
    fees[y-1][x-1]=cost
INF=10**18
costs=[[[INF,INF] for _ in range(n)] for _ in range(n)]
costs[0][0]=[0,0]
q=[]
heapq.heappush(q,(0,0,0,0))
while len(q)!=0:
    curcost,x,y,skipped=heapq.heappop(q)
    if x==n-1 and y==n-1:
        print(curcost)
        exit()
    for dx,dy in [(-1,0),(1,0),(0,-1),(0,1)]:
        if 0<=x+dx<=n-1 and 0<=y+dy<=n-1:
            if fees[y+dy][x+dx]!=0:
                if skipped==0:
                    if costs[y+dy][x+dx][skipped+1]>curcost+1:
                        heapq.heappush(q,(curcost+1,x+dx,y+dy,skipped+1))
                        costs[y+dy][x+dx][skipped+1]=curcost+1
                    if costs[y+dy][x+dx][skipped]>curcost+1+fees[y+dy][x+dx]:
                        heapq.heappush(q,(curcost+1+fees[y+dy][x+dx],x+dx,y+dy,skipped))
                        costs[y+dy][x+dx][skipped]=curcost+1+fees[y+dy][x+dx]
                else:
                    if costs[y+dy][x+dx][skipped]>curcost+1+fees[y+dy][x+dx]:
                        heapq.heappush(q,(curcost+1+fees[y+dy][x+dx],x+dx,y+dy,skipped))
                        costs[y+dy][x+dx][skipped]=curcost+1+fees[y+dy][x+dx]
            else:
                if costs[y+dy][x+dx][skipped]>curcost+1:
                    heapq.heappush(q,(curcost+1,x+dx,y+dy,skipped))
                    costs[y+dy][x+dx][skipped]=curcost+1