Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #3364855) | Cod sursa (job #3364645) | Cod sursa (job #3364672)
import heapq
f=open("dijkstra.in",'r')
w=open("dijkstra.out",'w')
INF = float('inf')
content=f.readline().split()
n,m=int(content[0]),int(content[1])
a=[[] for _ in range(n+1)]
for _ in range(m):
content=f.readline().split()
x,y,weight = int(content[0]),int(content[1]),int(content[2])
a[x].append((y,weight))
heap = []
viz = [0 for _ in range(n+1)]
dist = [INF for _ in range(n+1)]
heapq.heappush(heap,(0,1))
dist[1]=0
while heap:
cost,nod = heapq.heappop(heap)
if viz[nod]!=0:
continue
viz[nod]=1
for vecin,weight in a[nod]:
if dist[nod] + weight < dist[vecin]:
dist[vecin] = dist[nod] + weight
heapq.heappush(heap,(dist[vecin],vecin))
for x in range(2,len(dist)):
w.write(str(dist[x])+" ")
f.close()
w.close()