結果

問題 No.807 umg tours
ユーザー FromBooska
提出日時 2023-08-31 10:52:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,465 ms / 4,000 ms
コード長 1,393 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 169,240 KB
最終ジャッジ日時 2025-01-03 04:30:54
合計ジャッジ時間 17,874 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

# ダイクストラ
# ミラー頂点をN個作る、辺構造も同じ
# ミラーへは有向辺コスト0
# ダイクストラ1回で済ませられるか

N, M = map(int, input().split())
edges = [[] for i in range(N*2+1)]
for i in range(M):
    a, b, c = map(int, input().split())
    # 現世界内
    edges[a].append((b, c))
    edges[b].append((a, c))
    # ミラー世界内
    edges[a+N].append((b+N, c))
    edges[b+N].append((a+N, c))  
    # 現世界からミラー世界に有効辺コスト0
    edges[a].append((b+N, 0))
    edges[b].append((a+N, 0))

from heapq import heappush, heappop
INF = 10 ** 18
def dijkstra(s, n, connect): #(始点, ノード数)
    distance = [INF] * n
    que = [(0, s)] #(distance, node)
    distance[s] = 0
    confirmed = [False] * n # ノードが確定済みかどうか
    while que:
        w,v = heappop(que)
        if distance[v]<w:
            continue
        confirmed[v] = True
        for to, cost in connect[v]: # ノード v に隣接しているノードに対して
            if confirmed[to] == False and distance[v] + cost < distance[to]:
                distance[to] = distance[v] + cost
                heappush(que, (distance[to], to))
    return distance

distance = dijkstra(1,N*2+1, edges)

for i in range(1, N+1):
    if i == 1:
        ans = 0
    else:
        ans = distance[i]+distance[i+N]
    print(ans)


0