-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.py
More file actions
54 lines (40 loc) · 1.2 KB
/
Copy pathtask1.py
File metadata and controls
54 lines (40 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#task1
import math
from queue import PriorityQueue
task1_input = open('input1_1.txt','r')
task1_output = open('output1_1.txt','w')
nodes, edges = list(map(int,task1_input.readline().split()))
graph = {i: [] for i in range(1,nodes+1)}
for i in range(edges):
u,v,w = list(map(int,task1_input.readline().split()))
if graph[u] == 0:
graph[u] = [(v,w)]
else:
graph[u].append((v,w))
source = int(task1_input.readline())
# print(source)
# print(graph)
def shortest_path(source, graphs, c):
distance = [math.inf] * (nodes+1)
queue = PriorityQueue()
queue.put((c,source))
while not queue.empty():
weight, source = queue.get()
if distance[int(source)] > weight:
distance[int(source)] = weight
if graphs[int(source)] != 0:
for i in graphs[int(source)]:
source, new_value = i
queue.put((new_value + weight, source))
return distance
x = shortest_path(source, graph, 0)
print(x)
for i in range(1 ,len(x)) :
s = ''
if x[i] != math.inf :
s += str(x[i]) + ' '
task1_output.write(s)
else:
task1_output.write("-1")
task1_input.close()
task1_output.close()