結果

問題 No.160 最短経路のうち辞書順最小
ユーザー nanaenanae
提出日時 2017-05-27 14:58:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,035 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 11,980 KB
実行使用メモリ 13,736 KB
最終ジャッジ日時 2023-10-21 13:19:50
合計ジャッジ時間 2,647 ms
ジャッジサーバーID
(参考情報)
judge9 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,368 KB
testcase_01 AC 30 ms
10,368 KB
testcase_02 AC 30 ms
10,368 KB
testcase_03 AC 30 ms
10,368 KB
testcase_04 AC 45 ms
11,156 KB
testcase_05 AC 58 ms
11,752 KB
testcase_06 AC 71 ms
12,564 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 39 ms
10,668 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 37 ms
10,720 KB
testcase_20 AC 38 ms
10,736 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 32 ms
10,436 KB
testcase_28 WA -
testcase_29 AC 33 ms
10,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from heapq import heappush, heappop

inf = 1<<30

def solve():
    N, M, S, G = map(int, input().split())
    Adj = [[] for i in range(N)]

    for i in range(M):
        ai, bi, ci = map(int, input().split())
        Adj[ai].append((bi, ci))
        Adj[bi].append((ai, ci))

    d, p = dijkstra(N, Adj, S)

    keiro = [G]

    while p[G] != G:
        keiro.append(p[G])
        G = p[G]

    keiro.reverse()

    print(*keiro)

def dijkstra(N, Adj, s):
    d = [inf] * N
    p = [inf] * N
    visited = [False] * N
    d[s] = 0
    p[s] = s
    pq = [(0, s)]

    for i in range(N - 1):
        while pq:
            di, v = heappop(pq)

            if not visited[v] and di == d[v]:
                break

        visited[v] = True

        for u, c in Adj[v]:
            if d[v] + c < d[u]:
                d[u] = d[v] + c
                p[u] = v
                heappush(pq, (d[u], u))
            elif d[v] + c == d[u] and v < p[u]:
                p[u] = v

    return d, p

if __name__ == '__main__':
    solve()
0