結果

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

ソースコード

diff #

from heapq import *
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

# 同じ距離のときにルートの辞書順が小さい方を選べばいいので
# キーの2番目以降にルートを文字列でいれておいてダイクストラ
def main():
    n, m, s, g = MI()
    to = defaultdict(list)
    for _ in range(m):
        a, b, c = MI()
        to[a].append([b, c])
        to[b].append([a, c])
    hp = []
    heappush(hp, [0, s])
    fin = [False] * n
    while hp:
        d, *r = heappop(hp)
        u = r[-1]
        if u == g:
            print(*r)
            exit()
        if fin[u]: continue
        fin[u] = True
        for v, c in to[u]:
            if fin[v]: continue
            heappush(hp, [d + c] + r + [v])

main()
0