MST
View as PDFYou are tasked with finding the Minimum Spanning Tree of a Graph.
Input Format
The first line of input will consist of two integers, and
(
). The next
lines will consist of three integers
and
, representing an undirected edge between nodes
and
with weight
.
Output Format
Output a single integer, the weight of the MST of the given graph. If there is no MST, output -1.
Sample Input 1
3 4
1 2 5
1 3 6
2 3 2
2 1 3
Sample Output 1
5
Sample Explanation 1
The MST is constructed with the edges (2,1,3) and (2,3,2).
Comments