結果
| 問題 | No.1283 Extra Fee | 
| コンテスト | |
| ユーザー |  wgrape | 
| 提出日時 | 2024-11-14 17:23:49 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,687 ms / 2,000 ms | 
| コード長 | 1,365 bytes | 
| コンパイル時間 | 447 ms | 
| コンパイル使用メモリ | 82,320 KB | 
| 実行使用メモリ | 106,616 KB | 
| 最終ジャッジ日時 | 2024-11-14 17:24:14 | 
| 合計ジャッジ時間 | 22,251 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 30 | 
ソースコード
N,M = map(int,input().split())
G = [[1] * N for i in range(N)]
G[0][0] = 0
for _ in range(M):
    h,w,c = map(int,input().split())
    G[h - 1][w - 1] += c
    
import heapq as hq
q = []
hq.heappush(q, (0, 0, 0, 0))
visited = [[[False] * N for i in range(N)] for j in range(2)]
INF = 1 << 60
best = [[[INF] * N for i in range(N)] for j in range(2)]
cases = ((1,0),(0,1),(-1,0),(0,-1))
while q:
    c, used, i, j = hq.heappop(q)
    if visited[used][i][j]:
        continue
    visited[used][i][j] = True
    if i == N - 1 and j == N - 1:
        print(c)
        break
    for ci,cj in cases:
        ni = i + ci
        nj = j + cj
        if not (0 <= ni < N and 0 <= nj < N):
            continue
        if G[ni][nj] > 1 and used == 0: # まだ権利を使っていない
            if not visited[used + 1][ni][nj] and best[used + 1][ni][nj] > c + 1:
                hq.heappush(q, (c + 1, used + 1, ni, nj))
                best[used + 1][ni][nj] = c + 1
            if not visited[used][ni][nj] and best[used][ni][nj] > c + G[ni][nj]:
                hq.heappush(q, (c + G[ni][nj], used, ni, nj))
                best[used][ni][nj] = c + G[ni][nj]
        else:
            if not visited[used][ni][nj] and best[used][ni][nj] > c + G[ni][nj]:
                hq.heappush(q, (c + G[ni][nj], used, ni, nj))
                best[used][ni][nj] = c + G[ni][nj]
            
            
            
        