#User function Template for python3 import heapq class Solution: #Function to find sum of weights of edges of the Minimum Spanning Tree. def spanningTree(self, V, adj): #code here #MST sum_=0 adjl = adj vis = [0]*V pq = [] #wt,node #store parent as well when the mst is asked as well heapq.heappush(pq,[0,0]) while pq: cost,node = heapq.heappop(pq) if vis[node]==1:continue sum_+=cost vis[node] = 1 for ch,wt in adjl[node]: if vis[ch]==0:heapq.heappush(pq,[wt,ch]) return sum_
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter