P20923 [GeeksforGeeks] Minimum spanning tree
普及
题目描述

Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree.

Example 1:
Input:
3 3
0 1 5
1 2 3
0 2 1
Output:
4
Explanation:
The Spanning Tree resulting in a weight
of 4 is shown above.
Example 2:
Input:
2 1
0 1 5
Output:
5
Explanation:
Only one Spanning Tree is possible
which has a weight of 5.

Your task:
Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2]
In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt.

Expected Time Complexity: O(ElogV).
Expected Auxiliary Space: O(V^{2}).

Constraints:
2 ≤ V ≤ 1000
V-1 ≤ E ≤ (V*(V-1))/2
1 ≤ w ≤ 1000
The graph is connected and doesn't contain self-loops & multiple edges.

输入格式

见题面

输出格式

见题面

样例输入
3 3 0 1 5 1 2 3 0 2 1
样例输出
4