結果

問題 No.160 最短経路のうち辞書順最小
ユーザー rpy3cpprpy3cpp
提出日時 2015-06-02 13:57:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,035 ms / 5,000 ms
コード長 911 bytes
コンパイル時間 115 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 19,504 KB
最終ジャッジ日時 2023-09-20 18:44:50
合計ジャッジ時間 3,384 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,072 KB
testcase_01 AC 17 ms
8,144 KB
testcase_02 AC 16 ms
8,084 KB
testcase_03 AC 16 ms
8,044 KB
testcase_04 AC 28 ms
8,876 KB
testcase_05 AC 41 ms
9,288 KB
testcase_06 AC 51 ms
9,884 KB
testcase_07 AC 25 ms
8,608 KB
testcase_08 AC 24 ms
8,696 KB
testcase_09 AC 21 ms
8,492 KB
testcase_10 AC 27 ms
8,548 KB
testcase_11 AC 27 ms
8,764 KB
testcase_12 AC 25 ms
8,524 KB
testcase_13 AC 24 ms
8,508 KB
testcase_14 AC 25 ms
8,552 KB
testcase_15 AC 24 ms
8,520 KB
testcase_16 AC 22 ms
8,552 KB
testcase_17 AC 26 ms
8,688 KB
testcase_18 AC 24 ms
8,596 KB
testcase_19 AC 23 ms
8,628 KB
testcase_20 AC 23 ms
8,660 KB
testcase_21 AC 25 ms
8,612 KB
testcase_22 AC 25 ms
8,500 KB
testcase_23 AC 25 ms
8,572 KB
testcase_24 AC 25 ms
8,640 KB
testcase_25 AC 23 ms
8,468 KB
testcase_26 AC 24 ms
8,500 KB
testcase_27 AC 18 ms
8,176 KB
testcase_28 AC 1,035 ms
19,504 KB
testcase_29 AC 17 ms
8,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def read_data():
    N, M, S, G = map(int, input().split())
    Es = [dict() for i in range(N)]
    for m in range(M):
        a, b, c = map(int, input().split())
        Es[a][b] = c
        Es[b][a] = c
    return N, M, S, G, Es

def solve(N, M, start, goal, Es):
    dist = [float('inf')] * N
    path = [tuple() for n in range(N)]
    dist[start] = 0
    path[start] = (start, )
    pq = [(0, path[start])]
    while pq:
        d, pathi = heapq.heappop(pq)
        v = pathi[-1]
        if v == goal:
            return pathi
        for u, nd in Es[v].items():
            new_d = d + nd
            if (new_d < dist[u]) or (new_d == dist[u] and pathi + (u, ) < path[u]):
                dist[u] = new_d
                path[u] = pathi + (u, )
                heapq.heappush(pq, (new_d, path[u]))

if __name__ == '__main__':
    param = read_data()
    path = solve(*param)
    print(*path)
0