結果

問題 No.1283 Extra Fee
ユーザー ああいい
提出日時 2022-02-19 14:12:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,481 ms / 2,000 ms
コード長 1,091 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 120,796 KB
最終ジャッジ日時 2024-11-16 08:37:44
合計ジャッジ時間 20,390 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = map(int,input().split())
H = N
W = N
S = [[0] * W for _ in range(H)]
for i in range(M):
    h,w,c = map(int,input().split())
    S[h-1][w-1] = c
import heapq
inf = 10 ** 18
dist = [[[inf] * 2 for _ in range(W)] for _ in range(H)]
q  = [(0,0,0,0)]
dist[0][0][0] = 0
while q:
    d,h,w,flag = heapq.heappop(q)
    if dist[h][w][flag] < d:continue
    if h == N-1 and w == N -1:break
    for i,j in [(1,0),(-1,0),(0,1),(0,-1)]:
        if 0 <= h + i < H and 0 <= w + j < W:
            if S[h+i][w+j] == 0:
                if dist[h+i][w+j][flag] > d + 1:
                    dist[h+i][w+j][flag] = d + 1
                    heapq.heappush(q,(d+1,h+i,w+j,flag))
            else:
                c = S[h+i][w+j]
                if dist[h+i][w+j][flag] > d + 1 + c:
                    dist[h+i][w+j][flag] = d + 1 + c
                    heapq.heappush(q,(d+1+c,h+i,w+j,flag))
                if flag == 0:
                    if dist[h+i][w+j][1] > d + 1:
                        dist[h+i][w+j][1] = d + 1
                        heapq.heappush(q,(d+1,h+i,w+j,1))
print(dist[-1][-1][1])
0