Loading repovive.com/problems/classics/20
Given an undirected weighted graph with n vertices and m edges, find the weight of the Minimum Spanning Tree (MST) using Prim's algorithm.
Prim's algorithm starts from vertex 1 and greedily adds the minimum weight edge connecting the current tree to an unvisited vertex, using a priority queue.
If the graph is not connected, output -1.
4 5 1 2 1 1 3 3 2 3 2 2 4 4 3 4 5
For this input, the required output is 7.
7
3 3 1 2 1 2 3 2 1 3 3
For this input, the required output is 3.
3
4 2 1 2 5 3 4 3
For this input, the required output is -1.
-1