結果

問題 No.1301 Strange Graph Shortest Path
ユーザー zkouzkou
提出日時 2020-11-04 22:47:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,250 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 87,116 KB
実行使用メモリ 133,488 KB
最終ジャッジ日時 2023-10-10 21:34:22
合計ジャッジ時間 22,165 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,336 KB
testcase_01 AC 77 ms
71,172 KB
testcase_02 WA -
testcase_03 AC 507 ms
111,132 KB
testcase_04 AC 681 ms
130,632 KB
testcase_05 AC 492 ms
118,668 KB
testcase_06 AC 632 ms
122,752 KB
testcase_07 AC 609 ms
120,016 KB
testcase_08 AC 521 ms
111,124 KB
testcase_09 AC 616 ms
121,780 KB
testcase_10 WA -
testcase_11 AC 657 ms
124,064 KB
testcase_12 AC 639 ms
127,616 KB
testcase_13 AC 598 ms
119,956 KB
testcase_14 AC 619 ms
120,708 KB
testcase_15 AC 610 ms
121,396 KB
testcase_16 AC 686 ms
131,512 KB
testcase_17 AC 611 ms
120,172 KB
testcase_18 AC 569 ms
115,772 KB
testcase_19 AC 631 ms
124,960 KB
testcase_20 AC 644 ms
127,668 KB
testcase_21 AC 594 ms
120,620 KB
testcase_22 AC 642 ms
131,732 KB
testcase_23 AC 616 ms
118,992 KB
testcase_24 AC 678 ms
126,896 KB
testcase_25 AC 699 ms
128,152 KB
testcase_26 AC 614 ms
122,040 KB
testcase_27 AC 668 ms
123,728 KB
testcase_28 AC 530 ms
118,524 KB
testcase_29 WA -
testcase_30 AC 684 ms
126,088 KB
testcase_31 AC 680 ms
128,728 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 588 ms
124,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from heapq import * 

input = sys.stdin.readline

INF = 10 ** 18

N, M = map(int, input().split())
adj = [dict() for _ in range(N)]
uv2d = dict()
for _ in range(M):
    u, v, c, d = map(int, input().split())
    u -= 1
    v -= 1
    adj[u][v] = adj[v][u] = c
    uv2d[(u, v)] = uv2d[(v, u)] = d

# pq が持つのは、(コスト * N) + 頂点
# dijkstra 1回目
pq = [0]
dp = [INF] * N
dp[0] = 0
parent = [-1] * N
while pq:
    cost, v = divmod(heappop(pq), N)
    if cost > dp[v]:
        continue
    for nv, dist in adj[v].items():
        if dp[nv] > dp[v] + dist:
            dp[nv] = dp[v] + dist
            parent[nv] = v
            heappush(pq, (dp[nv] * N) + nv)

first_cost = dp[N - 1]

v = N - 1
while v != 0:
    p = parent[v]
    adj[v][p] = adj[p][v] = uv2d[(v, p)]
    v = p

# pq が持つのは、(コスト * N) + 頂点
# dijkstra 2回目
pq = [N - 1]
dp = [INF] * N
dp[N - 1] = 0
# parent = [-1] * N
while pq:
    cost, v = divmod(heappop(pq), N)
    if cost > dp[v]:
        continue
    for nv, dist in adj[v].items():
        if dp[nv] > dp[v] + dist:
            dp[nv] = dp[v] + dist
            # parent[nv] = v
            heappush(pq, (dp[nv] * N) + nv)

second_cost = dp[0]

print(first_cost + second_cost)
0