結果
問題 | No.1283 Extra Fee |
ユーザー | neterukun |
提出日時 | 2020-11-06 22:05:00 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,678 ms / 2,000 ms |
コード長 | 1,501 bytes |
コンパイル時間 | 1,035 ms |
コンパイル使用メモリ | 82,056 KB |
実行使用メモリ | 116,152 KB |
最終ジャッジ日時 | 2024-11-16 06:41:17 |
合計ジャッジ時間 | 19,154 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
import heapq dd = ((1, 0), (-1, 0), (0, 1), (0, -1)) def dijkstra() -> list: INF = 10 ** 18 dist = [[[INF] * n for i in range(n)] for i in range(2)] dist[0][0][0] = 0 q = [(0, 0, 0, 0)] # q = [(startからの距離, i, j, cnt)] while q: d, i, j, cnt = heapq.heappop(q) if dist[cnt][i][j] < d: continue for di, dj in dd: nxt_i, nxt_j = i + di, j + dj if not(0 <= nxt_i < h and 0 <= nxt_j < w): continue if cnt == 1: if dist[1][i][j] + 1 + cost[nxt_i][nxt_j] < dist[1][nxt_i][nxt_j]: dist[1][nxt_i][nxt_j] = dist[1][i][j] + 1 + cost[nxt_i][nxt_j] heapq.heappush(q, (dist[1][nxt_i][nxt_j], nxt_i, nxt_j, 1)) else: if dist[0][i][j] + 1 + cost[nxt_i][nxt_j] < dist[0][nxt_i][nxt_j]: dist[0][nxt_i][nxt_j] = dist[0][i][j] + 1 + cost[nxt_i][nxt_j] heapq.heappush(q, (dist[0][nxt_i][nxt_j], nxt_i, nxt_j, 0)) if dist[0][i][j] + 1 < dist[1][nxt_i][nxt_j]: dist[1][nxt_i][nxt_j] = dist[0][i][j] + 1 heapq.heappush(q, (dist[1][nxt_i][nxt_j], nxt_i, nxt_j, 1)) return dist n, m = map(int, input().split()) h, w = n, n info = [list(map(int, input().split())) for i in range(m)] cost = [[0] * n for i in range(n)] for i, j, c in info: i -= 1 j -= 1 cost[i][j] = c dist = dijkstra() print(dist[1][-1][-1])