結果
| 問題 |
No.2695 Warp Zone
|
| コンテスト | |
| ユーザー |
detteiuu
|
| 提出日時 | 2024-05-18 01:01:29 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 633 ms / 2,000 ms |
| コード長 | 1,017 bytes |
| コンパイル時間 | 162 ms |
| コンパイル使用メモリ | 82,860 KB |
| 実行使用メモリ | 191,048 KB |
| 最終ジャッジ日時 | 2024-12-20 15:56:59 |
| 合計ジャッジ時間 | 8,820 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
ソースコード
from heapq import heappush, heappop
from collections import defaultdict
H, W, N = map(int, input().split())
warp = [list(map(int, input().split())) for _ in range(N)]
G = defaultdict(list)
G[(1, 1)].append((H, W, (H-1)+(W-1)))
for i in range(N):
a, b, c, d = warp[i]
G[(1, 1)].append((a, b, (a-1)+(b-1)))
G[(a, b)].append((c, d, 1))
G[(c, d)].append((H, W, (H-c)+(W-d)))
for j in range(N):
a2, b2, c2, d2 = warp[j]
G[(c, d)].append((a2, b2, abs(a2-c)+abs(b2-d)))
def dijkstra(start):
dist = defaultdict(int)
dist[start] = 0
visited = set()
que = [(0, start)]
while que:
d, (h, w) = heappop(que)
if (h, w) in visited:
continue
visited.add((h, w))
for nh, nw, weight in G[(h, w)]:
if (nh, nw) not in dist or dist[(h, w)]+weight < dist[(nh, nw)]:
dist[(nh, nw)] = dist[(h, w)]+weight
heappush(que, (dist[(nh, nw)], (nh, nw)))
return dist
print(dijkstra((1, 1))[(H, W)])
detteiuu