結果
問題 | No.1283 Extra Fee |
ユーザー | tamato |
提出日時 | 2020-11-06 21:51:33 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 823 ms / 2,000 ms |
コード長 | 1,608 bytes |
コンパイル時間 | 329 ms |
コンパイル使用メモリ | 82,268 KB |
実行使用メモリ | 143,456 KB |
最終ジャッジ日時 | 2024-11-16 06:37:54 |
合計ジャッジ時間 | 11,371 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.buffer.readline import heapq def dijkstra(adj, start): # adj: [[to, cost] * vertices], 0th index must be empty inf = 1 << 60 dist = [inf] * len(adj) dist[start] = 0 q = [] heapq.heappush(q, (0, start)) while q: min_dist, v_from = heapq.heappop(q) if min_dist > dist[v_from]: continue v_tos = adj[v_from] for v_to, cost in v_tos: if min_dist + cost < dist[v_to]: dist[v_to] = min_dist + cost heapq.heappush(q, (dist[v_to], v_to)) return dist N, M = map(int, input().split()) grid = [[0] * (N+1) for _ in range(N+1)] for _ in range(M): h, w, c = map(int, input().split()) grid[h][w] = c d = [(0, 1), (0, -1), (1, 0), (-1, 0)] adj = [[] for _ in range(N*N + 1)] for h in range(1, N+1): for w in range(1, N+1): hw = (h-1) * N + w for dh, dw in d: h_new, w_new = h+dh, w+dw if 1 <= h_new <= N and 1 <= w_new <= N: hw_new = (h_new - 1) * N + w_new adj[hw].append((hw_new, grid[h_new][w_new] + 1)) dist1 = dijkstra(adj, 1) dist2 = dijkstra(adj, N*N) ans = float("inf") for h in range(1, N+1): for w in range(1, N+1): hw = (h-1) * N + w ans = min(ans, dist1[hw] + dist2[hw] - grid[h][w] * 2) print(ans) if __name__ == '__main__': main()