結果

問題 No.2695 Warp Zone
ユーザー sharusharu
提出日時 2024-03-23 02:08:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 567 ms / 2,000 ms
コード長 1,130 bytes
コンパイル時間 621 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 79,016 KB
最終ジャッジ日時 2024-03-23 02:08:31
合計ジャッジ時間 8,252 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
55,600 KB
testcase_01 AC 45 ms
55,600 KB
testcase_02 AC 45 ms
55,600 KB
testcase_03 AC 567 ms
78,504 KB
testcase_04 AC 518 ms
78,924 KB
testcase_05 AC 521 ms
79,016 KB
testcase_06 AC 559 ms
78,660 KB
testcase_07 AC 521 ms
78,800 KB
testcase_08 AC 77 ms
73,364 KB
testcase_09 AC 279 ms
78,628 KB
testcase_10 AC 46 ms
55,600 KB
testcase_11 AC 143 ms
76,708 KB
testcase_12 AC 328 ms
78,120 KB
testcase_13 AC 48 ms
55,600 KB
testcase_14 AC 380 ms
78,292 KB
testcase_15 AC 296 ms
78,828 KB
testcase_16 AC 142 ms
76,756 KB
testcase_17 AC 204 ms
77,852 KB
testcase_18 AC 452 ms
78,856 KB
testcase_19 AC 68 ms
68,372 KB
testcase_20 AC 414 ms
78,344 KB
testcase_21 AC 393 ms
78,260 KB
testcase_22 AC 251 ms
78,440 KB
testcase_23 AC 362 ms
78,236 KB
testcase_24 AC 46 ms
55,600 KB
testcase_25 AC 46 ms
55,600 KB
testcase_26 AC 47 ms
55,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from 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)] = 0
while 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)] + 1
            heappush(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)] + md
            heappush(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)] + md
        heappush(q, [dist[((h - 1, w - 1))], h - 1, w - 1])
print(dist[(h - 1, w - 1)])
0