結果

問題 No.1301 Strange Graph Shortest Path
ユーザー neterukunneterukun
提出日時 2020-11-27 22:17:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,554 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 87,080 KB
実行使用メモリ 129,444 KB
最終ジャッジ日時 2023-10-09 21:21:02
合計ジャッジ時間 21,211 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,128 KB
testcase_01 AC 70 ms
71,444 KB
testcase_02 WA -
testcase_03 AC 461 ms
106,884 KB
testcase_04 AC 598 ms
123,360 KB
testcase_05 AC 377 ms
108,436 KB
testcase_06 AC 569 ms
117,724 KB
testcase_07 AC 559 ms
114,724 KB
testcase_08 AC 492 ms
109,116 KB
testcase_09 AC 543 ms
117,616 KB
testcase_10 WA -
testcase_11 AC 586 ms
117,724 KB
testcase_12 AC 571 ms
119,692 KB
testcase_13 AC 532 ms
114,864 KB
testcase_14 AC 550 ms
116,000 KB
testcase_15 AC 542 ms
113,644 KB
testcase_16 AC 616 ms
123,668 KB
testcase_17 AC 546 ms
116,168 KB
testcase_18 AC 512 ms
111,424 KB
testcase_19 AC 580 ms
118,624 KB
testcase_20 AC 571 ms
119,820 KB
testcase_21 AC 581 ms
116,144 KB
testcase_22 AC 582 ms
122,088 KB
testcase_23 AC 583 ms
115,672 KB
testcase_24 AC 598 ms
120,404 KB
testcase_25 AC 601 ms
121,064 KB
testcase_26 AC 557 ms
115,384 KB
testcase_27 AC 594 ms
118,400 KB
testcase_28 AC 451 ms
109,400 KB
testcase_29 WA -
testcase_30 AC 638 ms
120,668 KB
testcase_31 AC 619 ms
122,252 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 574 ms
129,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input = sys.stdin.buffer.readline


def dijkstra(start: int, graph: list) -> list:
    """dijkstra法: 始点startから各頂点への最短距離を求める
    計算量: O((E+V)logV)
    """
    INF = 10 ** 18
    n = len(graph)
    dist = [INF] * n
    dist[start] = 0
    q = [(0, start)] # q = [(startからの距離, 現在地)]
    while q:
        d, v = heapq.heappop(q)
        if dist[v] < d:
            continue
        for nxt_v, cost in graph[v]:
            if dist[v] + cost < dist[nxt_v]:
                dist[nxt_v] = dist[v] + cost
                heapq.heappush(q, (dist[nxt_v], nxt_v))
    return dist


n, m = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(m)]
ans = 0


graph = [[] for i in range(n)]
for u, v, cost, _ in edges:
    u -= 1
    v -= 1
    graph[u].append((v, cost))
    graph[v].append((u, cost))

start = 0
dist = dijkstra(start, graph)
ans += dist[-1]

path = [n - 1]
v = n - 1
while v != 0:
    for prv_v, cost in graph[v]:
        if dist[prv_v] + cost == dist[v]:
            path.append(prv_v)
            v = prv_v
            break

mapping = set([])
for u, v in zip(path, path[1:]):
    mapping.add((u, v))
    mapping.add((v, u))


graph = [[] for i in range(n)]
for u, v, c, d in edges:
    u -= 1
    v -= 1
    if (u, v) in mapping:
        graph[u].append((v, d))
        graph[v].append((u, d))
    else:
        graph[u].append((v, c))
        graph[v].append((u, c))

start = 0
dist = dijkstra(start, graph)
ans += dist[-1]

print(ans)
0