結果

問題 No.2695 Warp Zone
ユーザー miya145592miya145592
提出日時 2024-03-22 22:56:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,560 ms / 2,000 ms
コード長 1,795 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 318,800 KB
最終ジャッジ日時 2024-03-22 22:56:58
合計ジャッジ時間 19,680 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,460 KB
testcase_01 AC 40 ms
53,460 KB
testcase_02 AC 40 ms
53,460 KB
testcase_03 AC 1,560 ms
318,800 KB
testcase_04 AC 1,444 ms
310,168 KB
testcase_05 AC 1,467 ms
310,920 KB
testcase_06 AC 1,484 ms
316,756 KB
testcase_07 AC 1,466 ms
318,136 KB
testcase_08 AC 67 ms
70,972 KB
testcase_09 AC 975 ms
152,372 KB
testcase_10 AC 40 ms
53,460 KB
testcase_11 AC 434 ms
95,316 KB
testcase_12 AC 1,007 ms
186,756 KB
testcase_13 AC 39 ms
53,460 KB
testcase_14 AC 1,144 ms
223,612 KB
testcase_15 AC 788 ms
139,696 KB
testcase_16 AC 352 ms
91,012 KB
testcase_17 AC 583 ms
111,352 KB
testcase_18 AC 1,235 ms
257,152 KB
testcase_19 AC 59 ms
68,508 KB
testcase_20 AC 1,201 ms
244,432 KB
testcase_21 AC 1,143 ms
221,568 KB
testcase_22 AC 712 ms
131,624 KB
testcase_23 AC 1,023 ms
185,420 KB
testcase_24 AC 39 ms
53,460 KB
testcase_25 AC 40 ms
53,460 KB
testcase_26 AC 40 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heappop

def dijkstra(s, N): # (始点, ノード数)
    dist = [INF for _ in range(N)]
    hq = [(0, s)]
    dist[s] = 0
    seen = [False] * N # ノードが確定済みかどうか
    while hq:
        d, v = heappop(hq) # ノードを pop する
        if seen[v]:
            continue
        seen[v] = True
        for to, cost in G[v]: # ノード v に隣接しているノードに対して
            if dist[v] + cost < dist[to]:
                dist[to] = dist[v] + cost
                heappush(hq, (dist[to], to))
    return dist

import sys
input = sys.stdin.readline
H, W, N = map(int, input().split())
ABCD = [list(map(int, input().split())) for _ in range(N)]
G = [[] for _ in range(2*N+2)]
INF = H+W
st = 2*N
ed = 2*N+1
G[st].append((ed, H-1+W-1))
G[ed].append((st, H-1+W-1))
for i in range(N):
    a, b, c, d = ABCD[i]
    u = i*2
    v = i*2+1
    G[u].append((v, 1))
    G[v].append((u, abs(c-a)+abs(d-b)))
    G[st].append((u, a-1+b-1))
    G[u].append((st, a-1+b-1))
    G[ed].append((u, H-a+W-b))
    G[u].append((ed, H-a+W-b))
    G[st].append((v, c-1+d-1))
    G[v].append((st, c-1+d-1))
    G[ed].append((v, H-c+W-d))
    G[v].append((ed, H-c+W-d))
for i in range(N):
    a, b, c, d = ABCD[i]
    u = i*2
    v = i*2+1
    for j in range(i+1, N):
        a2, b2, c2, d2 = ABCD[j]
        u2 = j*2
        v2 = j*2+1
        G[u].append((u2, abs(a2-a)+abs(b2-b)))
        G[u2].append((u, abs(a2-a)+abs(b2-b)))
        G[u].append((v2, abs(c2-a)+abs(d2-b)))
        G[v2].append((u, abs(c2-a)+abs(d2-b)))
        G[v].append((u2, abs(a2-c)+abs(b2-d)))
        G[u2].append((v, abs(a2-c)+abs(b2-d)))
        G[v].append((v2, abs(c2-c)+abs(d2-d)))
        G[v2].append((v, abs(c2-c)+abs(d2-d)))

dist = dijkstra(st, 2*N+2)
print(dist[ed])
0