結果

問題 No.160 最短経路のうち辞書順最小
ユーザー mkawa2mkawa2
提出日時 2020-01-24 15:29:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 135 ms / 5,000 ms
コード長 1,114 bytes
コンパイル時間 114 ms
コンパイル使用メモリ 11,044 KB
実行使用メモリ 20,296 KB
最終ジャッジ日時 2023-10-12 04:09:10
合計ジャッジ時間 2,219 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,736 KB
testcase_01 AC 19 ms
8,660 KB
testcase_02 AC 19 ms
8,656 KB
testcase_03 AC 19 ms
8,708 KB
testcase_04 AC 29 ms
9,828 KB
testcase_05 AC 41 ms
11,656 KB
testcase_06 AC 49 ms
12,464 KB
testcase_07 AC 27 ms
9,184 KB
testcase_08 AC 26 ms
9,364 KB
testcase_09 AC 24 ms
9,104 KB
testcase_10 AC 27 ms
9,320 KB
testcase_11 AC 26 ms
9,344 KB
testcase_12 AC 26 ms
9,180 KB
testcase_13 AC 25 ms
9,176 KB
testcase_14 AC 24 ms
9,124 KB
testcase_15 AC 25 ms
9,072 KB
testcase_16 AC 25 ms
9,180 KB
testcase_17 AC 27 ms
9,192 KB
testcase_18 AC 26 ms
9,096 KB
testcase_19 AC 26 ms
9,304 KB
testcase_20 AC 25 ms
9,176 KB
testcase_21 AC 26 ms
9,076 KB
testcase_22 AC 25 ms
9,196 KB
testcase_23 AC 26 ms
9,188 KB
testcase_24 AC 27 ms
9,356 KB
testcase_25 AC 25 ms
9,108 KB
testcase_26 AC 25 ms
9,168 KB
testcase_27 AC 21 ms
8,896 KB
testcase_28 AC 135 ms
20,296 KB
testcase_29 AC 21 ms
8,724 KB
権限があれば一括ダウンロードができます

ソースコード

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