結果

問題 No.160 最短経路のうち辞書順最小
ユーザー akasia_midoriakasia_midori
提出日時 2022-05-22 13:18:14
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,390 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 81,856 KB
実行使用メモリ 83,252 KB
最終ジャッジ日時 2023-10-20 16:57:54
合計ジャッジ時間 12,069 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,360 KB
testcase_01 AC 39 ms
53,360 KB
testcase_02 AC 35 ms
53,360 KB
testcase_03 AC 35 ms
53,360 KB
testcase_04 AC 79 ms
76,296 KB
testcase_05 AC 87 ms
76,860 KB
testcase_06 AC 96 ms
77,056 KB
testcase_07 AC 56 ms
68,732 KB
testcase_08 AC 4,682 ms
78,044 KB
testcase_09 AC 58 ms
70,796 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

def oi(): return int(input())
def os(): return input()
def mi(): return list(map(int, input().split()))
import sys
input = sys.stdin.readline

input_count = 0
N, M, START, GOAL = mi()

G = {i:[] for i in range(N)}
for i in range(M):
    A,B,C = mi()
    
    # Gの第三項に引き継ぎたいものを載せる
    # 何個目~ならi
    # 経路復元なら (A,B)など 行先だけ保存しておけばいいかも
    G[A].append((B, C))
    G[B].append((A, C))


# V: 頂点数
# g[v] = {(w, cost)}:
#     頂点vから遷移可能な頂点(w)とそのコスト(cost)
# r: 始点の頂点
 
from heapq import heappush, heappop
INF = 1<<55

# 経路復元の時はコメントアウト部分を解除
def dijkstra(N, G, s):
    dist = [INF] * N
    que = [(0, s)]
    dist[s] = 0
    
#     # 経路復元用
#     edge = {i:set([]) for i in range(N)}
    
#     # コスト同じものを持つか
#     reached = {i:{j:False for j in range(N)} for i in range(N)}
    
    while que:
        c, v = heappop(que)
        if dist[v] < c:
            continue
        for t, cost in G[v]:
#             if reached[v][t]:
#                 continue
            
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                
                # 経路復元用
#                 edge[t] = set([ind])
                heappush(que, (dist[t], t))
                
#                 reached[v][t] = True
            
            # コストが同じものもどうにかしたいならここ追加
#             elif dist[v] + cost == dist[t]:
#                 reached[v][t] = True
                
#                 edge[t].add(ind)
#                 heappush(que, (dist[t], t))            
    return dist#edge 

dist = dijkstra(N, G, START)

for k in G.keys():
    G[k] = sorted(G[k])

import heapq
deq = [[str(START).rjust(4,"0"), START, -1]]
heapq.heapify(deq)
ret = None
# count = 0
while deq:
    root, node, old_node = heapq.heappop(deq)
    
    if node == GOAL:
        ret =  root
        break
    for next_node, cost in G[node]:
        if next_node != old_node:
            if dist[next_node] - dist[node] == cost:
                hoge = str(next_node).rjust(4,"0")
                heapq.heappush(deq, (root+f" {hoge}", next_node, node))
    
#     count += 1
#     if count == 3:
#         print(deq)



print(*map(int, ret.split(" ")))
0