結果

問題 No.1301 Strange Graph Shortest Path
ユーザー neterukunneterukun
提出日時 2020-11-27 22:30:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,553 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 86,732 KB
実行使用メモリ 124,584 KB
最終ジャッジ日時 2023-10-09 21:32:13
合計ジャッジ時間 22,202 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
70,992 KB
testcase_01 AC 77 ms
71,172 KB
testcase_02 WA -
testcase_03 AC 532 ms
108,000 KB
testcase_04 AC 682 ms
123,316 KB
testcase_05 AC 485 ms
108,084 KB
testcase_06 AC 621 ms
117,616 KB
testcase_07 AC 621 ms
114,496 KB
testcase_08 AC 473 ms
107,296 KB
testcase_09 AC 610 ms
117,532 KB
testcase_10 WA -
testcase_11 AC 636 ms
118,492 KB
testcase_12 AC 664 ms
119,728 KB
testcase_13 AC 644 ms
113,868 KB
testcase_14 AC 594 ms
115,564 KB
testcase_15 AC 595 ms
114,224 KB
testcase_16 AC 704 ms
123,848 KB
testcase_17 AC 663 ms
116,244 KB
testcase_18 AC 606 ms
112,532 KB
testcase_19 AC 653 ms
119,168 KB
testcase_20 AC 640 ms
120,612 KB
testcase_21 AC 651 ms
115,876 KB
testcase_22 AC 635 ms
121,984 KB
testcase_23 AC 611 ms
114,280 KB
testcase_24 AC 659 ms
120,536 KB
testcase_25 AC 685 ms
120,976 KB
testcase_26 AC 636 ms
116,316 KB
testcase_27 AC 661 ms
117,712 KB
testcase_28 AC 521 ms
111,052 KB
testcase_29 WA -
testcase_30 AC 693 ms
120,596 KB
testcase_31 AC 677 ms
122,876 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 516 ms
120,468 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 = n - 1
dist = dijkstra(start, graph)
ans += dist[0]

path = [0]
v = 0
while v != n - 1:
    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