結果
問題 | No.807 umg tours |
ユーザー |
|
提出日時 | 2022-07-02 18:47:52 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 3,384 ms / 4,000 ms |
コード長 | 1,617 bytes |
コンパイル時間 | 4,336 ms |
コンパイル使用メモリ | 82,040 KB |
実行使用メモリ | 176,440 KB |
最終ジャッジ日時 | 2024-11-27 14:39:55 |
合計ジャッジ時間 | 33,387 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
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**15 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: #print(c1,j1,k) if dp[j1][1] > ndis: dp[j1][1] = ndis heappush(q, (ndis, j1, 1)) #print(dp) 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(fst) print(0) for i in range(1,n): print(fst[i]+dp[i][1])