結果

問題 No.160 最短経路のうち辞書順最小
ユーザー ckawatakckawatak
提出日時 2017-10-24 17:21:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 872 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 81,960 KB
実行使用メモリ 79,736 KB
最終ジャッジ日時 2024-05-01 13:10:00
合計ジャッジ時間 3,856 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
67,572 KB
testcase_01 AC 57 ms
67,920 KB
testcase_02 AC 55 ms
67,344 KB
testcase_03 AC 56 ms
67,652 KB
testcase_04 AC 112 ms
79,720 KB
testcase_05 AC 120 ms
79,736 KB
testcase_06 AC 117 ms
79,288 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 100 ms
79,004 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 101 ms
79,024 KB
testcase_20 AC 100 ms
79,124 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 74 ms
75,392 KB
testcase_28 WA -
testcase_29 AC 69 ms
73,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from queue import PriorityQueue

N,M,S,G = list(map(int, input().split(' ')))

C = []
for i in range(N):
    C.append([-1 for _ in range(N)])

for i in range(M):
    f,t,c = list(map(int, input().split(' ')))
    C[f][t] = c
    C[t][f] = c

D = []
for i in range(N):
    D.append(float('inf'))
D[S] = 0

P = []
for i in range(N):
    P.append(-1)

Q = PriorityQueue()
Q.put((0, S))

while not Q.empty():
    d, f = Q.get()
    if D[f] < d:
        continue
    for t, c in enumerate(C[f]):
        if c != -1:
            nc = D[f] + c 
            if nc < D[t]:
                D[t] = nc
                P[t] = f
                Q.put((D[t], t))
            elif nc == D[t]:
                P[t] = min(P[t], f)

s = S
t = G                
solution = []
while s != t:
    solution.insert(0, str(t))
    t = P[t]
solution.insert(0, str(S))

print(' '.join(solution))    
0