結果

問題 No.160 最短経路のうち辞書順最小
ユーザー nebukuro09nebukuro09
提出日時 2016-09-28 17:40:39
言語 Python2
(2.7.18)
結果
AC  
実行時間 2,013 ms / 5,000 ms
コード長 851 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 7,068 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-05-01 06:20:22
合計ジャッジ時間 3,925 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
6,812 KB
testcase_01 AC 11 ms
6,940 KB
testcase_02 AC 10 ms
6,948 KB
testcase_03 AC 11 ms
6,944 KB
testcase_04 AC 26 ms
7,424 KB
testcase_05 AC 40 ms
8,192 KB
testcase_06 AC 56 ms
9,088 KB
testcase_07 AC 24 ms
7,040 KB
testcase_08 AC 23 ms
7,040 KB
testcase_09 AC 17 ms
7,040 KB
testcase_10 AC 31 ms
7,040 KB
testcase_11 AC 32 ms
7,168 KB
testcase_12 AC 24 ms
6,944 KB
testcase_13 AC 24 ms
6,940 KB
testcase_14 AC 28 ms
6,940 KB
testcase_15 AC 24 ms
7,040 KB
testcase_16 AC 19 ms
7,040 KB
testcase_17 AC 27 ms
6,944 KB
testcase_18 AC 21 ms
6,948 KB
testcase_19 AC 19 ms
6,944 KB
testcase_20 AC 19 ms
7,040 KB
testcase_21 AC 22 ms
7,040 KB
testcase_22 AC 23 ms
7,040 KB
testcase_23 AC 23 ms
7,040 KB
testcase_24 AC 21 ms
7,168 KB
testcase_25 AC 22 ms
7,040 KB
testcase_26 AC 21 ms
7,040 KB
testcase_27 AC 13 ms
6,940 KB
testcase_28 AC 2,013 ms
12,160 KB
testcase_29 AC 12 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

N, M, S, G = map(int, raw_input().split())
rinsetsu = [[] for i in xrange(N)]
for _ in xrange(M):
    a, b, c = map(int, raw_input().split())
    rinsetsu[a].append((b, c))
    rinsetsu[b].append((a, c))

def dijkstra():
    dist = [(float('inf'), []) for i in xrange(N)]
    dist[S] = (0, [S])
    q = []

    for n, c in rinsetsu[S]:
        heapq.heappush(q, (c, n))
        dist[n] = (c, [S, n])

    while len(q) != 0:
        c, n = heapq.heappop(q)
        if c > dist[n][0]:
            continue
        for nn, nc in rinsetsu[n]:
            alt = dist[n][0] + nc
            if dist[nn][0] > alt or (dist[nn][0] == alt and dist[n][1]+[nn] < dist[nn][1]):
                dist[nn] = (alt, dist[n][1]+[nn])
                heapq.heappush(q, (alt, nn))
                
    return dist

print ' '.join(map(str, dijkstra()[G][1]))
0