結果

問題 No.160 最短経路のうち辞書順最小
ユーザー akasia_midoriakasia_midori
提出日時 2022-05-22 09:14:27
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,489 bytes
コンパイル時間 977 ms
コンパイル使用メモリ 81,808 KB
実行使用メモリ 78,684 KB
最終ジャッジ日時 2023-10-20 16:51:51
合計ジャッジ時間 4,134 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def oi(): return int(input())
def os(): return input()
def mi(): return list(map(int, input().split()))


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 

# V: 頂点数
# g[v] = {(w, cost)}:
#     頂点vから遷移可能な頂点(w)とそのコスト(cost)
# r: 始点の頂点
 
from heapq import heappush, heappop
INF = 10**10
def dijkstra(N, G, s):
    dist = [INF] * N
    que = [(0, s)]
    dist[s] = 0
    while que:
        c, v = heappop(que)
        if dist[v] < c:
            continue
        for t, cost in G[v]:
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                heappush(que, (dist[t], t))
    return dist

dist = dijkstra(N, G, START)

# 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
    
    while que:
        c, v = heappop(que)
        if dist[v] < c:
            continue
        for t, cost in G[v]:
            
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                heappush(que, (dist[t], t))
            
    return dist



deq = deque([[GOAL, -1, [GOAL]]])

from collections import deque
output = []
while deq:
    node, old_node, root = deq.pop()
    
    if node == START:
        output.append(root[::-1])
        continue
    
    for next_node, cost in G[node]:
        if next_node != old_node:
            if dist[node] - dist[next_node] == cost:
                temp = root[:]
                deq.append((next_node, node, temp+[next_node]))
                

ret = None
for row in output:
    strs = "_".join(list(map(str, row)))
    if ret is None:
        ret = strs
    else:
        ret = min(ret, strs) 

print(*ret.split("_"))
0