結果

問題 No.160 最短経路のうち辞書順最小
ユーザー akasia_midoriakasia_midori
提出日時 2022-05-22 04:10:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,458 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 81,776 KB
実行使用メモリ 85,144 KB
最終ジャッジ日時 2023-10-20 16:50:18
合計ジャッジ時間 4,792 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,556 KB
testcase_01 AC 37 ms
53,556 KB
testcase_02 AC 37 ms
53,556 KB
testcase_03 AC 37 ms
53,556 KB
testcase_04 AC 100 ms
77,212 KB
testcase_05 AC 113 ms
80,328 KB
testcase_06 AC 117 ms
80,720 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 85 ms
77,012 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 85 ms
77,012 KB
testcase_20 AC 85 ms
77,136 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 60 ms
73,144 KB
testcase_28 WA -
testcase_29 AC 47 ms
67,704 KB
権限があれば一括ダウンロードができます

ソースコード

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, f"{A}_{B}"))
    G[B].append((A, C, f"{B}_{A}"))


# 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, ind 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 edge 

ret = dijkstra(N, G, START)

START = START
GOAL = GOAL

# もし文字列でハッシュ化してたらここで解除
temp = ret.keys()
for k in temp:
    c = []
    for v in ret[k]:
        c.append(tuple(map(int, list(v.split("_")))))
    ret[k] = sorted(c)

def keiro_with_dijkstr(START, GOAL):
    
    def keiro_hukugen(node, old_node):
        keiro_list = []
        for next_node in ret[node]:
            nn = (set(next_node)-set([node])).pop()
            if nn != old_node:
                flg, keiro = keiro_hukugen(nn, node)
                if flg:
                    return flg, keiro + [node]

        if node == START:
            return True, [START]

        return False, keiro_list
    
    return keiro_hukugen(GOAL, -1)



print(*keiro_with_dijkstr(START, GOAL)[1])
0