結果
| 問題 | No.1283 Extra Fee |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-01 22:56:02 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 1,335 ms / 2,000 ms |
| コード長 | 956 bytes |
| 記録 | |
| コンパイル時間 | 248 ms |
| コンパイル使用メモリ | 84,992 KB |
| 実行使用メモリ | 125,596 KB |
| 最終ジャッジ日時 | 2026-03-22 18:09:00 |
| 合計ジャッジ時間 | 14,944 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
import heapq
INF = 1 << 60
n, m = map(int, input().split())
cost = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(m):
hi, wi, ci = map(int, input().split())
cost[hi - 1][wi - 1] = ci
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
dist = [[[INF for _ in range(2)] for _ in range(n)] for _ in range(n)]
dist[0][0][0] = 0
hp = [(0, 0, 0, 0)]
while len(hp) > 0:
cd, ci, cx, cy = heapq.heappop(hp)
if cd > dist[cx][cy][ci]:
continue
for d in range(4):
nx = cx + dx[d]
if nx < 0 or nx >= n:
continue
ny = cy + dy[d]
if ny < 0 or ny >= n:
continue
if dist[nx][ny][ci] > dist[cx][cy][ci] + cost[nx][ny] + 1:
dist[nx][ny][ci] = dist[cx][cy][ci] + cost[nx][ny] + 1
heapq.heappush(hp, (dist[nx][ny][ci], ci, nx, ny))
if ci == 0 and dist[nx][ny][1] > dist[cx][cy][ci] + 1:
dist[nx][ny][1] = dist[cx][cy][ci] + 1
heapq.heappush(hp, (dist[nx][ny][1], 1, nx, ny))
print(min(dist[-1][-1]))