結果

問題 No.160 最短経路のうち辞書順最小
ユーザー ckawatakckawatak
提出日時 2017-10-24 19:39:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 135 ms / 5,000 ms
コード長 870 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 79,784 KB
最終ジャッジ日時 2024-05-01 13:10:47
合計ジャッジ時間 4,321 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
68,364 KB
testcase_01 AC 62 ms
67,676 KB
testcase_02 AC 62 ms
68,248 KB
testcase_03 AC 62 ms
68,116 KB
testcase_04 AC 125 ms
79,720 KB
testcase_05 AC 135 ms
79,784 KB
testcase_06 AC 135 ms
79,580 KB
testcase_07 AC 105 ms
79,220 KB
testcase_08 AC 107 ms
79,200 KB
testcase_09 AC 108 ms
78,928 KB
testcase_10 AC 106 ms
79,336 KB
testcase_11 AC 110 ms
79,124 KB
testcase_12 AC 111 ms
79,244 KB
testcase_13 AC 107 ms
79,000 KB
testcase_14 AC 106 ms
78,960 KB
testcase_15 AC 103 ms
79,352 KB
testcase_16 AC 114 ms
79,256 KB
testcase_17 AC 107 ms
78,916 KB
testcase_18 AC 108 ms
79,288 KB
testcase_19 AC 109 ms
79,256 KB
testcase_20 AC 112 ms
79,500 KB
testcase_21 AC 104 ms
79,248 KB
testcase_22 AC 106 ms
78,952 KB
testcase_23 AC 113 ms
79,356 KB
testcase_24 AC 115 ms
79,360 KB
testcase_25 AC 108 ms
79,444 KB
testcase_26 AC 107 ms
79,220 KB
testcase_27 AC 81 ms
75,944 KB
testcase_28 AC 117 ms
79,368 KB
testcase_29 AC 71 ms
73,504 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

P = []
for i in range(N):
    P.append(-1)
    
D = []
for i in range(N):
    D.append(float('inf'))
D[G] = 0

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

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.append(str(s))
    s = P[s]
solution.append(str(G))

print(' '.join(solution))    
0