結果

問題 No.1283 Extra Fee
ユーザー roaris
提出日時 2020-11-14 02:55:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,512 ms / 2,000 ms
コード長 1,049 bytes
コンパイル時間 510 ms
コンパイル使用メモリ 82,408 KB
実行使用メモリ 101,248 KB
最終ジャッジ日時 2024-11-16 07:12:32
合計ジャッジ時間 16,508 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from heapq import *

def dijkstra():
    dist = [[[10**18]*N for _ in range(N)] for _ in range(2)]
    dist[0][0][0] = 0
    pq = []
    heappush(pq, (0, 0, 0, 0))
    
    while pq:
        d, f, x, y = heappop(pq)
        
        if dist[f][x][y]<d:
            continue
        
        for nx, ny in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]:
            if 0<=nx<N and 0<=ny<N:
                if dist[f][nx][ny]>dist[f][x][y]+cost[nx][ny]:
                    dist[f][nx][ny] = dist[f][x][y]+cost[nx][ny]
                    heappush(pq, (dist[f][nx][ny], f, nx, ny))
                
                if f==0 and cost[nx][ny]>1 and dist[1][nx][ny]>dist[0][x][y]+1:
                    dist[1][nx][ny] = dist[0][x][y]+1
                    heappush(pq, (dist[1][nx][ny], 1, nx, ny))
    
    return min(dist[0][-1][-1], dist[1][-1][-1])

N, M = map(int, input().split())
cost = [[1]*N for _ in range(N)]

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

print(dijkstra())
0