結果
問題 | No.807 umg tours |
ユーザー |
|
提出日時 | 2022-07-02 18:52:58 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 3,623 ms / 4,000 ms |
コード長 | 1,806 bytes |
コンパイル時間 | 450 ms |
コンパイル使用メモリ | 82,668 KB |
実行使用メモリ | 177,008 KB |
最終ジャッジ日時 | 2025-03-23 17:13:54 |
合計ジャッジ時間 | 35,579 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
def main(): from heapq import heappop, heappush from sys import stdin n,m = map(int,input().split()) abcs = [[] for i in range(n)] for i in range(m): a,b,c = map(int,input().split()) a -= 1 b -= 1 abcs[a].append([b,c]) abcs[b].append([a,c]) INF = 10**14 dp = [[INF]*2 for i in range(n)] dp[0][0] = 0 dp[0][1] = 0 q = [(0, 0, 0)]#c, idx, use while q: c, j, use = heappop(q) abc = abcs[j] if dp[j][use] != c: continue for k in range(len(abc)): j1,c1 = abc[k] ndis = c1 + c if use == 0: if dp[j1][0] > ndis: dp[j1][0] = ndis heappush(q, (ndis, j1, 0)) if dp[j1][1] > c: dp[j1][1] = c heappush(q, (c, j1, 1)) else: if dp[j1][1] > ndis: dp[j1][1] = ndis heappush(q, (ndis, j1, 1)) def dijkstra(s, n): # (始点, ノード数) dist = [INF] * n hq = [(0, s)] # (distance, node) dist[s] = 0 seen = [False] * n # ノードが確定済みかどうか while hq: tmp = heappop(hq) # ノードを pop する v = tmp[1] c = tmp[0] seen[v] = True if dist[v] != c: continue for to, cost in abcs[v]: # ノード v に隣接しているノードに対して if seen[to] == False and dist[v] + cost < dist[to]: dist[to] = dist[v] + cost heappush(hq, (dist[to], to)) return dist fst = dijkstra(0,n) print(0) for i in range(1,n): print(fst[i]+dp[i][1]) main()