結果
| 問題 | No.1283 Extra Fee | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-02-01 22:51:59 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,793 ms / 2,000 ms | 
| コード長 | 976 bytes | 
| コンパイル時間 | 243 ms | 
| コンパイル使用メモリ | 81,852 KB | 
| 実行使用メモリ | 102,964 KB | 
| 最終ジャッジ日時 | 2024-07-01 15:48:18 | 
| 合計ジャッジ時間 | 20,878 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| 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(n)] for _ in range(n)] for _ in range(2)]
dist[0][0][0] = 0
hp = [(0, 0, 0, 0)]
while len(hp) > 0:
  cd, ci, cx, cy = heapq.heappop(hp)
  if cd > dist[ci][cx][cy]:
    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[ci][nx][ny] > dist[ci][cx][cy] + cost[nx][ny] + 1:
      dist[ci][nx][ny] = dist[ci][cx][cy] + cost[nx][ny] + 1
      heapq.heappush(hp, (dist[ci][nx][ny], ci, nx, ny))
    if ci == 0 and dist[1][nx][ny] > dist[ci][cx][cy] + 1:
      dist[1][nx][ny] = dist[ci][cx][cy] + 1
      heapq.heappush(hp, (dist[1][nx][ny], 1, nx, ny))
print(min(dist[0][-1][-1], dist[1][-1][-1]))
            
            
            
        