結果

問題 No.160 最短経路のうち辞書順最小
ユーザー titiatitia
提出日時 2022-03-30 01:19:50
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 134 ms / 5,000 ms
コード長 636 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-04-26 10:09:46
合計ジャッジ時間 2,366 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,880 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 30 ms
10,880 KB
testcase_03 AC 31 ms
11,008 KB
testcase_04 AC 51 ms
11,520 KB
testcase_05 AC 70 ms
12,288 KB
testcase_06 AC 91 ms
13,184 KB
testcase_07 AC 40 ms
11,264 KB
testcase_08 AC 43 ms
11,136 KB
testcase_09 AC 41 ms
11,264 KB
testcase_10 AC 47 ms
11,392 KB
testcase_11 AC 47 ms
11,264 KB
testcase_12 AC 43 ms
11,136 KB
testcase_13 AC 41 ms
11,136 KB
testcase_14 AC 43 ms
11,264 KB
testcase_15 AC 43 ms
11,136 KB
testcase_16 AC 40 ms
11,264 KB
testcase_17 AC 42 ms
11,264 KB
testcase_18 AC 42 ms
11,136 KB
testcase_19 AC 41 ms
11,264 KB
testcase_20 AC 43 ms
11,264 KB
testcase_21 AC 41 ms
11,264 KB
testcase_22 AC 42 ms
11,136 KB
testcase_23 AC 44 ms
11,392 KB
testcase_24 AC 44 ms
11,264 KB
testcase_25 AC 42 ms
11,136 KB
testcase_26 AC 40 ms
11,136 KB
testcase_27 AC 33 ms
11,008 KB
testcase_28 AC 134 ms
14,208 KB
testcase_29 AC 32 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop,heappush

N,M,S,G=map(int,input().split())
E=[[] for i in range(N+1)]

for i in range(M):
    a,b,c=map(int,input().split())
    E[a].append((b,c))
    E[b].append((a,c))
    
D=[1<<30]*(N+1)
FROM=[-1]*(N+1)
D[G]=0
Q=[]
Q.append((0,G))

while Q:
    time,now=heappop(Q)

    if D[now]!=time:
        continue

    for to,cost in E[now]:
        if D[to]>time+cost:
            D[to]=time+cost
            heappush(Q,(D[to],to))
            FROM[to]=now
        elif D[to]==time+cost:
            FROM[to]=min(FROM[to],now)
        
now=S
ANS=[S]
while now!=G:
    now=FROM[now]
    ANS.append(now)

print(*ANS)
0