-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprim.py
58 lines (41 loc) · 1.15 KB
/
prim.py
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
55
56
57
58
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import defaultdict
from heapq import heappop, heappush, heapify
# Complete the prims function below.
def prims(n, edges, start):
graph = defaultdict(list)
for edge in edges:
begin, finish, weight = edge
graph[begin].append((weight, finish))
graph[finish].append((weight, begin))
hq = graph[start]
heapify(hq)
reached = [False]*(n+1)
reached[start] = True
total_weight = 0
while hq:
weight, edge = heappop(hq)
if not reached[edge]:
total_weight += weight
reached[edge] = True
for neighbour in graph[edge]:
if not reached[neighbour[1]]:
heappush(hq, neighbour)
return total_weight
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nm = input().split()
n = int(nm[0])
m = int(nm[1])
edges = []
for _ in range(m):
edges.append(list(map(int, input().rstrip().split())))
start = int(input())
result = prims(n, edges, start)
fptr.write(str(result) + '\n')
fptr.close()