結果

問題 No.160 最短経路のうち辞書順最小
ユーザー rlangevinrlangevin
提出日時 2023-08-16 12:33:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 276 ms / 5,000 ms
コード長 813 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 87,056 KB
実行使用メモリ 84,568 KB
最終ジャッジ日時 2023-08-16 12:33:29
合計ジャッジ時間 8,564 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,560 KB
testcase_01 AC 90 ms
71,804 KB
testcase_02 AC 91 ms
71,880 KB
testcase_03 AC 92 ms
71,828 KB
testcase_04 AC 276 ms
79,868 KB
testcase_05 AC 244 ms
81,968 KB
testcase_06 AC 251 ms
82,300 KB
testcase_07 AC 232 ms
79,148 KB
testcase_08 AC 232 ms
78,860 KB
testcase_09 AC 233 ms
78,892 KB
testcase_10 AC 229 ms
79,160 KB
testcase_11 AC 232 ms
79,336 KB
testcase_12 AC 232 ms
78,976 KB
testcase_13 AC 242 ms
78,996 KB
testcase_14 AC 233 ms
79,240 KB
testcase_15 AC 231 ms
79,180 KB
testcase_16 AC 235 ms
79,096 KB
testcase_17 AC 233 ms
79,248 KB
testcase_18 AC 237 ms
78,976 KB
testcase_19 AC 234 ms
79,120 KB
testcase_20 AC 237 ms
79,308 KB
testcase_21 AC 230 ms
79,028 KB
testcase_22 AC 229 ms
78,904 KB
testcase_23 AC 233 ms
79,016 KB
testcase_24 AC 234 ms
79,064 KB
testcase_25 AC 229 ms
78,852 KB
testcase_26 AC 232 ms
79,172 KB
testcase_27 AC 210 ms
78,184 KB
testcase_28 AC 254 ms
84,568 KB
testcase_29 AC 208 ms
79,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *

N, M, S, G = map(int, input().split())
inf = 10 ** 18
dp = [[inf] * N for i in range(N)]
for i in range(N):
    dp[i][i] = 0

Edge = defaultdict(lambda:inf)
for i in range(M):
    a, b, c = map(int, input().split())
    dp[a][b] = min(dp[a][b], c)
    dp[b][a] = min(dp[b][a], c)
    Edge[(a, b)] = min(Edge[(a, b)], c)
    Edge[(b, a)] = min(Edge[(b, a)], c)

for k in range(N):
    for i in range(N):
        for j in range(N):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

ans = [S]
now = S
cost = dp[now][G]
while now != G:
    for i in range(N):
        if i == now:
            continue
        if dp[now][i] + dp[i][G] == cost and Edge[now, i] == dp[now][i]:
            ans.append(i)
            cost -= dp[now][i]
            now = i
            break

print(*ans)
0