-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.py
More file actions
37 lines (26 loc) · 725 Bytes
/
Copy pathtask1.py
File metadata and controls
37 lines (26 loc) · 725 Bytes
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
#task1
task1_input = open('input1_2.txt',"r")
task1_output = open('output1_2.txt',"w")
N, M = map(int, task1_input.readline().split())
graph = {i: [] for i in range(1, N + 1)}
for i in range(M):
u, v, w = map(int, task1_input.readline().split())
graph[u].append((v,w))
graph[v].append((u,w))
print(graph)
total_cost = 0
source = 1
store = {source}
while len(store) < N:
min_cost = float('inf')
min_city = None
for x in store:
for k, v in graph[x]:
if k not in store and v < min_cost :
min_cost= v
min_city= k
store.add(min_city)
total_cost += min_cost
task1_output.write(str(total_cost) )
task1_input.close()
task1_output.close()