結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,492 KB
testcase_01 AC 78 ms
71,684 KB
testcase_02 WA -
testcase_03 AC 485 ms
111,120 KB
testcase_04 AC 683 ms
130,696 KB
testcase_05 AC 469 ms
118,484 KB
testcase_06 AC 618 ms
123,076 KB
testcase_07 AC 584 ms
119,908 KB
testcase_08 AC 484 ms
111,232 KB
testcase_09 AC 609 ms
121,724 KB
testcase_10 WA -
testcase_11 AC 663 ms
124,100 KB
testcase_12 AC 632 ms
127,256 KB
testcase_13 AC 577 ms
119,752 KB
testcase_14 AC 599 ms
120,772 KB
testcase_15 AC 590 ms
121,788 KB
testcase_16 AC 666 ms
131,444 KB
testcase_17 AC 602 ms
120,256 KB
testcase_18 AC 554 ms
115,912 KB
testcase_19 AC 619 ms
124,816 KB
testcase_20 AC 630 ms
127,520 KB
testcase_21 AC 585 ms
120,476 KB
testcase_22 AC 618 ms
131,796 KB
testcase_23 AC 593 ms
119,020 KB
testcase_24 AC 662 ms
126,968 KB
testcase_25 AC 680 ms
127,940 KB
testcase_26 AC 575 ms
122,000 KB
testcase_27 AC 620 ms
123,752 KB
testcase_28 AC 483 ms
118,760 KB
testcase_29 WA -
testcase_30 AC 651 ms
126,068 KB
testcase_31 AC 648 ms
128,612 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 569 ms
124,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from heapq import * 

input = sys.stdin.readline

INF = 10 ** 15

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