結果

問題 No.160 最短経路のうち辞書順最小
ユーザー tnodinotnodino
提出日時 2022-07-24 19:47:39
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 101 ms / 5,000 ms
コード長 651 bytes
コンパイル時間 72 ms
コンパイル使用メモリ 10,936 KB
実行使用メモリ 11,936 KB
最終ジャッジ日時 2023-09-20 19:49:46
合計ジャッジ時間 2,236 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,476 KB
testcase_01 AC 18 ms
8,660 KB
testcase_02 AC 18 ms
8,504 KB
testcase_03 AC 18 ms
8,500 KB
testcase_04 AC 41 ms
9,216 KB
testcase_05 AC 72 ms
10,128 KB
testcase_06 AC 101 ms
10,656 KB
testcase_07 AC 26 ms
8,812 KB
testcase_08 AC 28 ms
8,936 KB
testcase_09 AC 26 ms
8,796 KB
testcase_10 AC 28 ms
8,916 KB
testcase_11 AC 30 ms
8,980 KB
testcase_12 AC 28 ms
9,028 KB
testcase_13 AC 26 ms
8,828 KB
testcase_14 AC 27 ms
8,980 KB
testcase_15 AC 26 ms
8,824 KB
testcase_16 AC 28 ms
8,916 KB
testcase_17 AC 27 ms
9,028 KB
testcase_18 AC 27 ms
8,996 KB
testcase_19 AC 28 ms
8,892 KB
testcase_20 AC 29 ms
9,028 KB
testcase_21 AC 25 ms
8,808 KB
testcase_22 AC 26 ms
8,852 KB
testcase_23 AC 29 ms
9,060 KB
testcase_24 AC 31 ms
8,912 KB
testcase_25 AC 27 ms
8,896 KB
testcase_26 AC 26 ms
8,984 KB
testcase_27 AC 20 ms
8,708 KB
testcase_28 AC 98 ms
11,936 KB
testcase_29 AC 19 ms
8,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
INF = 1<<64
N,M,S,T = 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))
for i in range(N):
    G[i].sort()
cost = [INF] * N
cost[T] = 0
Queue = deque()
Queue.append(T)
while Queue:
    pos = Queue.popleft()
    for nxt,c in G[pos]:
        if cost[pos] + c < cost[nxt]:
            cost[nxt] = cost[pos] + c
            Queue.append(nxt)
pos = S
ans = [S]
while pos != T:
    for nxt,c in G[pos]:
        if cost[pos] - c == cost[nxt]:
            ans.append(nxt)
            pos = nxt
            break
print(*ans)
0