init
This commit is contained in:
commit
851751ea87
82 changed files with 2093 additions and 0 deletions
31
3G.py
Normal file
31
3G.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import sys
|
||||
import heapq
|
||||
data = sys.stdin.read().split()
|
||||
if data:
|
||||
n = int(data[0])
|
||||
m = int(data[1])
|
||||
graph = [[] for _ in range(n + 1)]
|
||||
idx = 2
|
||||
for _ in range(m):
|
||||
u = int(data[idx])
|
||||
v = int(data[idx+1])
|
||||
w = int(data[idx+2])
|
||||
graph[u].append((v, w))
|
||||
graph[v].append((u, w))
|
||||
idx += 3
|
||||
inf = 10**18
|
||||
dist = [inf] * (n + 1)
|
||||
dist[1] = 0
|
||||
pq = [(0, 1)]
|
||||
while len(pq) > 0:
|
||||
d, u = heapq.heappop(pq)
|
||||
if d > dist[u]:
|
||||
continue
|
||||
for v, w in graph[u]:
|
||||
if dist[u] + w < dist[v]:
|
||||
dist[v] = dist[u] + w
|
||||
heapq.heappush(pq, (dist[v], v))
|
||||
ans = []
|
||||
for i in range(1, n + 1):
|
||||
ans.append(str(dist[i]))
|
||||
sys.stdout.write(" ".join(ans) + "\n")
|
||||
Loading…
Add table
Add a link
Reference in a new issue