結果

問題 No.160 最短経路のうち辞書順最小
ユーザー akasia_midoriakasia_midori
提出日時 2022-05-22 15:09:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 92 ms / 5,000 ms
コード長 1,262 bytes
コンパイル時間 179 ms
コンパイル使用メモリ 81,884 KB
実行使用メモリ 77,692 KB
最終ジャッジ日時 2023-10-20 17:02:23
合計ジャッジ時間 3,095 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,564 KB
testcase_01 AC 34 ms
53,564 KB
testcase_02 AC 36 ms
53,564 KB
testcase_03 AC 38 ms
53,564 KB
testcase_04 AC 78 ms
76,800 KB
testcase_05 AC 85 ms
76,768 KB
testcase_06 AC 92 ms
76,744 KB
testcase_07 AC 75 ms
74,180 KB
testcase_08 AC 72 ms
74,024 KB
testcase_09 AC 68 ms
73,304 KB
testcase_10 AC 70 ms
73,692 KB
testcase_11 AC 71 ms
74,632 KB
testcase_12 AC 71 ms
74,188 KB
testcase_13 AC 67 ms
73,184 KB
testcase_14 AC 65 ms
73,184 KB
testcase_15 AC 67 ms
73,184 KB
testcase_16 AC 69 ms
74,864 KB
testcase_17 AC 64 ms
73,184 KB
testcase_18 AC 68 ms
73,672 KB
testcase_19 AC 70 ms
74,008 KB
testcase_20 AC 76 ms
76,640 KB
testcase_21 AC 69 ms
73,184 KB
testcase_22 AC 69 ms
73,184 KB
testcase_23 AC 73 ms
76,436 KB
testcase_24 AC 75 ms
76,588 KB
testcase_25 AC 65 ms
73,548 KB
testcase_26 AC 65 ms
73,184 KB
testcase_27 AC 43 ms
61,620 KB
testcase_28 AC 91 ms
77,692 KB
testcase_29 AC 43 ms
55,612 KB
権限があれば一括ダウンロードができます

ソースコード

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



dist = [INF] * N
def dijkstra(N, G, s):
    
    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