forked from tanus786/CP-Codes-HackOctober-Fest-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum Spanning tree in C++
64 lines (62 loc) · 1.33 KB
/
Maximum Spanning tree in C++
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
#include <bits/stdc++.h>
using namespace std;
int printheap(int N){return 0;}
int set_min_vertex(vector<int> value, vector<bool> set_mst)
{
int minimum = INT_MAX, vertex,a=value.size();
for (int i = 0; i < a; i++)
{
if (set_mst[i] == false && value[i] < minimum)
{
vertex = i;
minimum = value[i];
}
}
return vertex;
}
int find_mst(vector<vector<int>> graph)
{
int v=graph.size();
int parent[v];
vector<int> value(v, INT_MAX);
vector<bool> set_mst(v, false);
parent[0] = -1;
value[0] = 0;
for (int i = 0; i < v - 1; i++)
{
int u = set_min_vertex(value, set_mst);
set_mst[u] = true;
for (int j = 0; j < v; j++)
{
if (graph[u][j] != 0 && set_mst[j] == false && graph[u][j] < value[j])
{
value[j] = graph[u][j];
parent[j] = u;
}
}
}
int sum=0;
for (int i = 1; i < v; i++)
{
sum+= graph[i][parent[i]];
}
return sum;
}
int main()
{
int t,n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
vector<vector<int>> vec(n,vector<int>(n));
while(n--)
{
int a,b,c;
cin>>a>>b>>c;
vec[a-1][b-1]=vec[b-1][a-1]=-c;
}
cout<<-find_mst(vec)<<endl;
}
return 0;
}