局域网
思路:模板题直接kruskal,只是把构成最小生成树以外的边相加输出
具体代码如下
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110, M = 210;
struct Node{
int a, b, w;
bool operator< (const Node& W) const{
return w < W.w;
}
}node[M];
int n, k;
int p[N];
int find(int x){
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main(){
cin >> n >> k;
for(int i=1; i<=n; ++i) p[i] = i;
for(int i=0; i<k; ++i){
int a, b, w;
cin >> a >> b >> w;
node[i] = {a, b, w};
}
sort(node, node + k);
int res = 0;
for(int i=0; i<k; ++i){
int a = node[i].a, b = node[i].b, w = node[i].w;
a = find(a), b = find(b);
if(a != b) p[a] = b;
else res += w;
}
cout << res << endl;
return 0;
}