結果

問題 No.160 最短経路のうち辞書順最小
ユーザー akasia_midoriakasia_midori
提出日時 2022-05-22 15:08:35
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,611 bytes
コンパイル時間 144 ms
コンパイル使用メモリ 81,884 KB
実行使用メモリ 78,872 KB
最終ジャッジ日時 2023-10-20 17:02:19
合計ジャッジ時間 4,069 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def oi(): return int(input())
def os(): return input()
def mi(): return list(map(int, input().split()))
# import sys
# input = sys.stdin.readline

input_count = 0
N, M, START, GOAL = mi()

G = {i:[] for i in range(N)}
for i in range(M):
    A,B,C = mi()
    
    # Gの第三項に引き継ぎたいものを載せる
    # 何個目~ならi
    # 経路復元なら (A,B)など 行先だけ保存しておけばいいかも
    G[A].append((B, C))
    G[B].append((A, C))


# V: 頂点数
# g[v] = {(w, cost)}:
#     頂点vから遷移可能な頂点(w)とそのコスト(cost)
# r: 始点の頂点
 
from heapq import heappush, heappop
INF = 1<<55


# def trace(s, t, ancestors):
#     route = [t]
#     c = t
#     while True:
#         a = ancestors[c]
#         assert a is not None, 'Failed to trace'
#         route.append(a)
#         if a == s:
#             break
#         c = ancestors[c]
#     route.reverse()
#     return route

# 経路復元の時はコメントアウト部分を解除
def dijkstra(N, G, s):
    dist = [INF] * N
    que = [(0, s)]
    dist[s] = 0
    
    while que:
        c, v = heappop(que)
        if dist[v] < c:
            continue
        for t, cost in G[v]:
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                heappush(que, (dist[t], t))
                
    return dist

d1 = dijkstra(N, G, GOAL)

ret = []
now = START
while now!=GOAL:
    
    mins = INF
    ret.append(now)
    for v,c in G[now]:
        if dist[v] + c == dist[now]:
             mins = min(mins, v)
    now = mins
ret.append(GOAL)

print(*ret)
0