結果

問題 No.160 最短経路のうち辞書順最小
ユーザー DrDrpilotDrDrpilot
提出日時 2022-06-25 12:21:36
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 132 ms / 5,000 ms
コード長 844 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2024-04-26 18:33:06
合計ジャッジ時間 2,285 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,008 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 27 ms
10,880 KB
testcase_03 AC 26 ms
11,008 KB
testcase_04 AC 44 ms
11,776 KB
testcase_05 AC 60 ms
12,416 KB
testcase_06 AC 76 ms
13,184 KB
testcase_07 AC 34 ms
11,264 KB
testcase_08 AC 36 ms
11,392 KB
testcase_09 AC 34 ms
11,264 KB
testcase_10 AC 38 ms
11,392 KB
testcase_11 AC 41 ms
11,264 KB
testcase_12 AC 38 ms
11,136 KB
testcase_13 AC 37 ms
11,136 KB
testcase_14 AC 36 ms
11,264 KB
testcase_15 AC 35 ms
11,136 KB
testcase_16 AC 35 ms
11,136 KB
testcase_17 AC 35 ms
11,264 KB
testcase_18 AC 34 ms
11,264 KB
testcase_19 AC 36 ms
11,264 KB
testcase_20 AC 36 ms
11,136 KB
testcase_21 AC 33 ms
11,264 KB
testcase_22 AC 34 ms
11,136 KB
testcase_23 AC 36 ms
11,392 KB
testcase_24 AC 38 ms
11,392 KB
testcase_25 AC 38 ms
11,136 KB
testcase_26 AC 37 ms
11,136 KB
testcase_27 AC 29 ms
11,008 KB
testcase_28 AC 132 ms
14,336 KB
testcase_29 AC 31 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
n,m,S,G=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))
dst=[10**18]*n
cfm=[False]*n
pre=[None]*n
q=[]
heapq.heappush(q,(0,G,-1))
while q:
    now_d,now_place,pre_place=heapq.heappop(q)
    if cfm[now_place]:
        continue
    pre[now_place]=pre_place
    cfm[now_place]=True
    dst[now_place]=now_d
    for to_place,cost in g[now_place]:
        if cfm[to_place]:
            continue
        if dst[to_place]<=now_d+cost:
            continue
        dst[to_place]=now_d+cost
        heapq.heappush(q,(now_d+cost,to_place,now_place))
now=S;ans=[]
while now!=G:
    tmp=10**18
    ans.append(now)
    for to,cost in g[now]:
        if dst[to]+cost==dst[now]:
            tmp=min(tmp,to)
    now=tmp
ans.append(G)
print(*ans)
0