結果

問題 No.160 最短経路のうち辞書順最小
ユーザー convexineqconvexineq
提出日時 2020-12-12 13:27:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 101 ms / 5,000 ms
コード長 853 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 81,628 KB
実行使用メモリ 76,784 KB
最終ジャッジ日時 2023-10-20 02:07:10
合計ジャッジ時間 3,592 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
53,364 KB
testcase_01 AC 43 ms
53,364 KB
testcase_02 AC 40 ms
53,364 KB
testcase_03 AC 40 ms
53,364 KB
testcase_04 AC 87 ms
76,196 KB
testcase_05 AC 93 ms
76,784 KB
testcase_06 AC 101 ms
76,676 KB
testcase_07 AC 74 ms
72,860 KB
testcase_08 AC 74 ms
72,828 KB
testcase_09 AC 74 ms
72,828 KB
testcase_10 AC 72 ms
72,812 KB
testcase_11 AC 75 ms
72,824 KB
testcase_12 AC 74 ms
72,828 KB
testcase_13 AC 71 ms
70,764 KB
testcase_14 AC 71 ms
70,764 KB
testcase_15 AC 70 ms
70,764 KB
testcase_16 AC 76 ms
72,856 KB
testcase_17 AC 71 ms
70,764 KB
testcase_18 AC 72 ms
72,828 KB
testcase_19 AC 74 ms
72,828 KB
testcase_20 AC 77 ms
72,860 KB
testcase_21 AC 70 ms
70,764 KB
testcase_22 AC 70 ms
70,764 KB
testcase_23 AC 75 ms
72,832 KB
testcase_24 AC 78 ms
72,916 KB
testcase_25 AC 73 ms
72,828 KB
testcase_26 AC 71 ms
70,764 KB
testcase_27 AC 48 ms
55,416 KB
testcase_28 AC 95 ms
76,616 KB
testcase_29 AC 45 ms
55,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *
def dijkstra(g,start):
    n = len(g)
    INF = 1<<61
    dist = [INF]*(n) #startからの最短距離
    #pending = n-1 #未確定の点の個数
    dist[start] = 0
    q = [(0,start)] #(そこまでの距離、点)
    while q:# and pending:
        dv,v = heappop(q)
        if dist[v] < dv: continue
        for to, cost in g[v]:
            if dv + cost < dist[to]:
                dist[to] = dv + cost
                heappush(q, (dist[to], to))
    return dist

n,m,s,goal = map(int,input().split())
g = [[] for _ in range(n)]
for _ in range(m):
    a,b,c = map(int,input().split())
    g[a].append((b,c))
    g[b].append((a,c))
    
dist = dijkstra(g,goal)

res = [s]
v = s
while v != goal:
    i = n+1
    for u,c in g[v]:
        if dist[v] == dist[u] + c:
            i = min(i,u)
    v = i
    res.append(v)
print(*res)
0