結果
問題 | No.2695 Warp Zone |
ユーザー |
![]() |
提出日時 | 2024-03-23 02:08:21 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 430 ms / 2,000 ms |
コード長 | 1,130 bytes |
コンパイル時間 | 375 ms |
コンパイル使用メモリ | 82,232 KB |
実行使用メモリ | 79,892 KB |
最終ジャッジ日時 | 2024-09-30 13:00:56 |
合計ジャッジ時間 | 6,437 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
from collections import defaultdictfrom heapq import *h, w, n = map(int, input().split())warp = dict()for _ in range(n):a, b, c, d = map(lambda x: int(x) - 1, input().split())warp[(a, b)] = (c, d)q = [[0, 0, 0]]INF = float("INF")dist = defaultdict(lambda: INF)dist[(0, 0)] = 0while q:dis, i, j = heappop(q)if (i, j) in dist and dist[(i, j)] != dis:continue# warpを使う場合if (i, j) in warp:ti, tj = warp[(i, j)]if dist[(ti, tj)] > dist[(i, j)] + 1:dist[(ti, tj)] = dist[(i, j)] + 1heappush(q, [dist[(ti, tj)], ti, tj])# i,j から別のワープ a,b まで移動for (a, b), (c, d) in warp.items():md = abs(i - a) + abs(j - b)if dist[(a, b)] > dist[(i, j)] + md:dist[(a, b)] = dist[(i, j)] + mdheappush(q, [dist[(a, b)], a, b])# H,Wまで移動md = abs(i - h + 1) + abs(j - w + 1)if dist[(h - 1, w - 1)] > dist[(i, j)] + md:dist[(h - 1, w - 1)] = dist[(i, j)] + mdheappush(q, [dist[((h - 1, w - 1))], h - 1, w - 1])print(dist[(h - 1, w - 1)])