結果

問題 No.160 最短経路のうち辞書順最小
ユーザー maspymaspy
提出日時 2020-03-04 14:06:22
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 84 ms / 5,000 ms
コード長 889 bytes
コンパイル時間 237 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 17,312 KB
最終ジャッジ日時 2024-04-22 01:37:27
合計ジャッジ時間 2,781 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,752 KB
testcase_01 AC 31 ms
10,752 KB
testcase_02 AC 31 ms
11,008 KB
testcase_03 AC 31 ms
10,880 KB
testcase_04 AC 42 ms
12,160 KB
testcase_05 AC 53 ms
13,396 KB
testcase_06 AC 61 ms
14,720 KB
testcase_07 AC 38 ms
11,392 KB
testcase_08 AC 37 ms
11,648 KB
testcase_09 AC 36 ms
11,264 KB
testcase_10 AC 38 ms
11,520 KB
testcase_11 AC 39 ms
11,648 KB
testcase_12 AC 38 ms
11,392 KB
testcase_13 AC 37 ms
11,392 KB
testcase_14 AC 37 ms
11,520 KB
testcase_15 AC 37 ms
11,264 KB
testcase_16 AC 37 ms
11,392 KB
testcase_17 AC 38 ms
11,648 KB
testcase_18 AC 38 ms
11,392 KB
testcase_19 AC 37 ms
11,392 KB
testcase_20 AC 37 ms
11,648 KB
testcase_21 AC 36 ms
11,264 KB
testcase_22 AC 37 ms
11,520 KB
testcase_23 AC 38 ms
11,776 KB
testcase_24 AC 38 ms
11,520 KB
testcase_25 AC 37 ms
11,520 KB
testcase_26 AC 38 ms
11,264 KB
testcase_27 AC 33 ms
10,880 KB
testcase_28 AC 84 ms
17,312 KB
testcase_29 AC 33 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heappush

# %%
N, M, S, G = map(int, readline().split())
m = map(int, read().split())
ABC = zip(m, m, m)

# %%
graph = [[] for _ in range(N)]
for a, b, c in ABC:
    graph[a].append((b, c))
    graph[b].append((a, c))

# %%
INF = 10 ** 15
dist = [INF] * N
dist[G] = 0
q = [G]
while q:
    x = heappop(q)
    dv, v = divmod(x, 1 << 20)
    if dv > dist[v]:
        continue
    for w, cost in graph[v]:
        dw = dv + cost
        if dw >= dist[w]:
            continue
        dist[w] = dw
        heappush(q, (dw << 20) + w)


# %%
path = [S]
while path[-1] != G:
    v = path[-1]
    for w, cost in sorted(graph[v]):
        if dist[v] - cost == dist[w]:
            path.append(w)
            break
print(*path)
0