Pagini recente » Cod sursa (job #1629011) | Cod sursa (job #3287107) | Cod sursa (job #1338203) | Cod sursa (job #1337815) | Cod sursa (job #2636185)
'''
# Sample code to perform I/O:
name = input() # Reading input from STDIN
print('Hi, %s.' % name) # Writing output to STDOUT
# Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
'''
# Write your code here
import heapq
from collections import defaultdict
f = open("dijkstra.in","r")
line = f.readline().split(" ")
v = int(line[0])
e = int(line[1])
graph = [ [] for _ in range(v)]
for _ in range(e):
line = f.readline().split(" ")
graph[int(line[0])-1].append( [int(line[1])-1,int(line[2])] )
f.close()
hq = []
heapq.heappush(hq, (0, 0))
sol = defaultdict(lambda: 0)
while hq:
val, node = heapq.heappop(hq)
if node not in sol:
sol[node] = val
for entry in graph[node]:
n, d = entry
if n in sol:
continue
else:
heapq.heappush(hq, (d+val, n))
with open("dijkstra.out","w") as f:
for n in range(1, v):
f.write(str(sol[n]))
f.write(" ")