結果

問題 No.160 最短経路のうち辞書順最小
ユーザー maspy
提出日時 2020-03-04 14:06:22
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 76 ms / 5,000 ms
コード長 889 bytes
コンパイル時間 357 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 17,316 KB
最終ジャッジ日時 2024-10-14 00:14:06
合計ジャッジ時間 2,436 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heappush

# %%
N, M, S, G = map(int, readline().split())
m = map(int, read().split())
ABC = zip(m, m, m)

# %%
graph = [[] for _ in range(N)]
for a, b, c in ABC:
    graph[a].append((b, c))
    graph[b].append((a, c))

# %%
INF = 10 ** 15
dist = [INF] * N
dist[G] = 0
q = [G]
while q:
    x = heappop(q)
    dv, v = divmod(x, 1 << 20)
    if dv > dist[v]:
        continue
    for w, cost in graph[v]:
        dw = dv + cost
        if dw >= dist[w]:
            continue
        dist[w] = dw
        heappush(q, (dw << 20) + w)


# %%
path = [S]
while path[-1] != G:
    v = path[-1]
    for w, cost in sorted(graph[v]):
        if dist[v] - cost == dist[w]:
            path.append(w)
            break
print(*path)
0