結果

問題 No.1283 Extra Fee
ユーザー qibqib
提出日時 2023-02-01 22:56:02
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 956 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 87,340 KB
実行使用メモリ 124,860 KB
最終ジャッジ日時 2023-09-14 08:27:36
合計ジャッジ時間 26,047 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
70,892 KB
testcase_01 AC 74 ms
71,328 KB
testcase_02 AC 75 ms
71,408 KB
testcase_03 AC 75 ms
71,196 KB
testcase_04 AC 75 ms
71,472 KB
testcase_05 AC 76 ms
71,072 KB
testcase_06 AC 79 ms
71,196 KB
testcase_07 AC 75 ms
70,908 KB
testcase_08 AC 78 ms
71,396 KB
testcase_09 AC 77 ms
71,212 KB
testcase_10 AC 79 ms
71,232 KB
testcase_11 AC 268 ms
81,128 KB
testcase_12 AC 263 ms
81,280 KB
testcase_13 AC 223 ms
79,920 KB
testcase_14 AC 393 ms
83,808 KB
testcase_15 AC 499 ms
87,148 KB
testcase_16 AC 251 ms
80,292 KB
testcase_17 AC 1,959 ms
121,152 KB
testcase_18 AC 1,443 ms
110,012 KB
testcase_19 AC 1,433 ms
109,784 KB
testcase_20 AC 1,375 ms
107,852 KB
testcase_21 AC 1,351 ms
108,300 KB
testcase_22 AC 1,200 ms
104,896 KB
testcase_23 AC 1,261 ms
105,564 KB
testcase_24 AC 1,270 ms
105,388 KB
testcase_25 AC 1,475 ms
111,168 KB
testcase_26 AC 1,468 ms
111,164 KB
testcase_27 AC 1,507 ms
110,600 KB
testcase_28 AC 1,484 ms
111,776 KB
testcase_29 TLE -
権限があれば一括ダウンロードができます

ソースコード

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