結果
問題 | No.807 umg tours |
ユーザー |
![]() |
提出日時 | 2023-02-17 17:34:02 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,272 ms / 4,000 ms |
コード長 | 1,659 bytes |
コンパイル時間 | 162 ms |
コンパイル使用メモリ | 82,460 KB |
実行使用メモリ | 169,196 KB |
最終ジャッジ日時 | 2024-07-19 09:55:33 |
合計ジャッジ時間 | 14,998 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
# 頂点倍化# 0コストで動ける各頂点のダミー頂点というのを作っておく# ダミーの世界は真の世界のと同じで、真の世界と同じコストの辺を張る# ダイクストラ上、真の世界とダミーの世界を複数回行き来するのは意味がない、なされない# 答えは、真の世界でのゴールまでのコスト+min(真の世界ゴールまでのコスト、ダミー世界ゴールまでのコスト)# これでダイクストラ1回ですべての答えが求まるN, M = map(int, input().split())edges = [[] for i in range(N*2)]for i in range(M):a, b, c = map(int, input().split())a -= 1b -= 1edges[a].append((b, c))edges[b].append((a, c))edges[a+N].append((b+N, c))edges[b+N].append((a+N, c))edges[a].append((b+N, 0))edges[b].append((a+N, 0))from heapq import heappush, heappopINF = 10 ** 18def dijkstra(s, n, connect): #(始点, ノード数)distance = [INF] * nque = [(0, s)] #(distance, node)distance[s] = 0confirmed = [False] * n # ノードが確定済みかどうかwhile que:w,v = heappop(que)if distance[v]<w:continueconfirmed[v] = Truefor to, cost in connect[v]: # ノード v に隣接しているノードに対してif confirmed[to] == False and distance[v] + cost < distance[to]:distance[to] = distance[v] + costheappush(que, (distance[to], to))return distancedistance = dijkstra(0, N*2, edges)for i in range(N):ans = distance[i] + min(distance[i], distance[i+N])print(ans)