結果

問題 No.160 最短経路のうち辞書順最小
ユーザー brthyyjpbrthyyjp
提出日時 2021-02-27 01:36:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 96 ms / 5,000 ms
コード長 1,048 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 82,340 KB
実行使用メモリ 77,568 KB
最終ジャッジ日時 2024-04-10 15:01:58
合計ジャッジ時間 3,351 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,480 KB
testcase_01 AC 37 ms
52,992 KB
testcase_02 AC 37 ms
52,736 KB
testcase_03 AC 37 ms
52,864 KB
testcase_04 AC 69 ms
69,248 KB
testcase_05 AC 79 ms
73,088 KB
testcase_06 AC 96 ms
77,568 KB
testcase_07 AC 57 ms
64,512 KB
testcase_08 AC 60 ms
65,152 KB
testcase_09 AC 65 ms
67,200 KB
testcase_10 AC 59 ms
64,512 KB
testcase_11 AC 61 ms
65,408 KB
testcase_12 AC 64 ms
67,584 KB
testcase_13 AC 61 ms
65,792 KB
testcase_14 AC 56 ms
64,512 KB
testcase_15 AC 55 ms
64,000 KB
testcase_16 AC 60 ms
66,048 KB
testcase_17 AC 55 ms
64,640 KB
testcase_18 AC 58 ms
65,024 KB
testcase_19 AC 61 ms
65,792 KB
testcase_20 AC 63 ms
66,560 KB
testcase_21 AC 57 ms
64,000 KB
testcase_22 AC 56 ms
64,384 KB
testcase_23 AC 61 ms
65,920 KB
testcase_24 AC 66 ms
67,200 KB
testcase_25 AC 59 ms
64,896 KB
testcase_26 AC 58 ms
64,128 KB
testcase_27 AC 44 ms
54,400 KB
testcase_28 AC 74 ms
74,752 KB
testcase_29 AC 42 ms
53,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

n, m, s, g = map(int, input().split())
edge = [[] for i in range(n)]
for i in range(m):
    a, b, c = map(int, input().split())
    edge[a].append((c, b))
    edge[b].append((c, a))

edge = [sorted(l, key=lambda x: x[1]) for l in edge]

import heapq
INF = 10**18
def dijkstra(s, edge):
    n = len(edge)
    dist = [INF]*n
    prev = [-1]*n
    dist[s] = 0
    edgelist = []
    heapq.heappush(edgelist,(dist[s], s))
    while edgelist:
        minedge = heapq.heappop(edgelist)
        if dist[minedge[1]] < minedge[0]:
            continue
        v = minedge[1]
        for e in edge[v]:
            if dist[e[1]] > dist[v]+e[0]:
                dist[e[1]] = dist[v]+e[0]
                prev[e[1]] = v
                heapq.heappush(edgelist,(dist[e[1]], e[1]))
    return dist, prev

d, p = dijkstra(g, edge)
v = s
ans = [s]
while v != g:
    for c, u in edge[v]:
        if d[u]+c == d[v]:
            v = u
            break
    ans.append(v)
print(*ans)
0