結果
| 問題 | No.1283 Extra Fee | 
| コンテスト | |
| ユーザー |  titia | 
| 提出日時 | 2020-11-06 22:39:19 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,613 ms / 2,000 ms | 
| コード長 | 1,087 bytes | 
| コンパイル時間 | 232 ms | 
| コンパイル使用メモリ | 82,156 KB | 
| 実行使用メモリ | 102,552 KB | 
| 最終ジャッジ日時 | 2024-11-16 06:46:49 | 
| 合計ジャッジ時間 | 17,837 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 30 | 
ソースコード
import sys
input = sys.stdin.readline
N,M=map(int,input().split())
DP=[[1<<60]*N for i in range(N)]
DP2=[[1<<60]*N for i in range(N)]
MAP=[[1]*N for i in range(N)]
for i in range(M):
    h,w,c=map(int,input().split())
    h-=1
    w-=1
    MAP[h][w]+=c
DP[0][0]=0
import heapq
# Q=[(c,x,y,f)]
Q=[(0,0,0,0)]
while Q:
    c,x,y,f=heapq.heappop(Q)
    if f==0 and c>DP[x][y]:
        continue
    if f==1 and c>DP2[x][y]:
        continue
    if f==1:
        for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
            if 0<=z<N and 0<=w<N and c+MAP[z][w]<DP2[z][w]:
                DP2[z][w]=c+MAP[z][w]
                heapq.heappush(Q,(DP2[z][w],z,w,1))
    else:
        for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
            if 0<=z<N and 0<=w<N and c+MAP[z][w]<DP[z][w]:
                DP[z][w]=c+MAP[z][w]
                heapq.heappush(Q,(DP[z][w],z,w,0))
            if 0<=z<N and 0<=w<N and MAP[z][w]!=1 and c+1<DP2[z][w]:
                DP2[z][w]=c+1
                heapq.heappush(Q,(DP2[z][w],z,w,1))
        
print(min(DP[N-1][N-1],DP2[N-1][N-1]))
            
            
            
            
        