-
Notifications
You must be signed in to change notification settings - Fork 80
/
prim_mst_special_subtree.py
65 lines (49 loc) · 1.68 KB
/
prim_mst_special_subtree.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
59
60
61
62
63
64
65
#!/bin/python3
"""
https://www.hackerrank.com/challenges/primsmstsub/problem
"""
from collections import defaultdict
import heapq
import os
import sys
class UndirectedGraph:
def __init__(self, num_vertices):
self.num_vertices = num_vertices
def find_minimum_spanning_tree_weight(self, start):
total_weight = 0
num_edges = 0
min_heap = [(0, start, start), ] # (weight, source, destination)
visited = set()
while min_heap or (num_edges < self.num_vertices - 1):
# The minimum-weight edge which connects to a new vertex.
weight, u, v = heapq.heappop(min_heap)
if v not in visited:
total_weight += weight
num_edges += 1
visited.add(v)
for neighbor, weight in self.outgoing_edges[v].items():
if neighbor not in visited:
heapq.heappush(min_heap, (weight, v, neighbor))
return total_weight
def prims(n, edges, start):
graph = UndirectedGraph(n)
graph.outgoing_edges = edges
return graph.find_minimum_spanning_tree_weight(start)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nm = input().split()
n = int(nm[0])
m = int(nm[1])
edges = defaultdict(dict)
for _ in range(m):
src, des, weight = list(map(int, sys.stdin.readline().split()))
# Node indexes are 1-based.
src = src - 1
des = des - 1
# the graph is undirected.
edges[src][des] = weight
edges[des][src] = weight
start = int(input())
result = prims(n, edges, start - 1)
fptr.write(str(result) + '\n')
fptr.close()