結果

問題 No.1283 Extra Fee
ユーザー qibqib
提出日時 2023-02-01 22:51:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,899 ms / 2,000 ms
コード長 976 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 86,640 KB
実行使用メモリ 106,148 KB
最終ジャッジ日時 2023-09-14 08:24:09
合計ジャッジ時間 23,479 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,352 KB
testcase_01 AC 77 ms
71,372 KB
testcase_02 AC 78 ms
71,184 KB
testcase_03 AC 76 ms
71,240 KB
testcase_04 AC 77 ms
71,168 KB
testcase_05 AC 77 ms
71,052 KB
testcase_06 AC 80 ms
71,356 KB
testcase_07 AC 78 ms
71,320 KB
testcase_08 AC 82 ms
71,204 KB
testcase_09 AC 79 ms
71,160 KB
testcase_10 AC 78 ms
71,048 KB
testcase_11 AC 275 ms
80,156 KB
testcase_12 AC 256 ms
79,804 KB
testcase_13 AC 221 ms
79,020 KB
testcase_14 AC 368 ms
81,092 KB
testcase_15 AC 453 ms
81,780 KB
testcase_16 AC 249 ms
79,408 KB
testcase_17 AC 1,772 ms
105,648 KB
testcase_18 AC 1,254 ms
93,580 KB
testcase_19 AC 1,223 ms
92,208 KB
testcase_20 AC 1,194 ms
91,984 KB
testcase_21 AC 1,168 ms
91,600 KB
testcase_22 AC 1,040 ms
89,844 KB
testcase_23 AC 1,076 ms
88,220 KB
testcase_24 AC 1,098 ms
87,540 KB
testcase_25 AC 1,267 ms
93,924 KB
testcase_26 AC 1,265 ms
93,384 KB
testcase_27 AC 1,264 ms
92,284 KB
testcase_28 AC 1,272 ms
93,732 KB
testcase_29 AC 1,899 ms
106,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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]))
0