結果

問題 No.1995 CHIKA Road
ユーザー lloyz
提出日時 2022-07-01 22:01:12
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 942 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 82,188 KB
実行使用メモリ 1,698,368 KB
最終ジャッジ日時 2024-11-26 05:05:41
合計ジャッジ時間 100,654 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 MLE * 1
other AC * 3 TLE * 2 MLE * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from heapq import heapify, heappop, heappush

n, m = map(int, input().split())
Edge = defaultdict(list)
A, B = set(), set()
for _ in range(m):
    a, b = map(int, input().split())
    Edge[a - 1].append((b - 1, 2 * b - 2 * a - 1))
    A.add(a - 1)
    B.add(b - 1)
for f in A:
    for t in A:
        if f < t:
            Edge[f].append((t, 2 * (t - f)))
    Edge[f].append((n - 1, 2 * (n - 1 - f)))
for f in B:
    for t in A:
        if f < t:
            Edge[f].append((t, 2 * (t - f)))
    Edge[f].append((n - 1, 2 * (n - 1 - f)))

INF = 10**18
DP = defaultdict(lambda: INF)
DP[0] = 0
H = [(0, 0)]
heapify(H)
while H:
    ccost, cp = heappop(H)
    if ccost > DP[cp]:
        continue
    if cp == n - 1:
        print(ccost)
        break
    for np, dcost in Edge[cp]:
        if ccost + dcost >= DP[np]:
            continue
        DP[np] = ccost + dcost
        heappush(H, (ccost + dcost, np))
0