```cpp
void makeset()
{
for(int i=1;i<=m;i++)
{
uset[i]=i;
}
}
```
这里,并查集初始化错了,应该将m改成n。(一共有n个点)
by S_Gloria @ 2019-08-07 20:50:10
大佬大佬help me!
```
#include<iostream>
#include<cstdio>
#include<queue>//队列
#include<cmath>
#include<stack>//栈
#include<cstring>
#include<algorithm>
int f[1005],c=0,sum=0;
int n,m,k;
struct node
{
int u,v,w;
}e[10005];
using namespace std;
bool cmp(node x,node y)
{
return x.w<y.w;
}
void init(int n)
{
for(int i=1;i<=n;i++)
{
f[i]=i;
}
return;
}
int getf(int e)
{
if(f[e]==e) return f[e];
else
{
return getf(f[e]);
}
}
int merge(int x,int y)
{
int ans1,ans2;
ans1=getf(x);
ans2=getf(y);
if(ans1!=ans2)
{
f[ans2]=ans1;
return 1;//没联通,可以进行合并
}
return 0;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m>>k;
for(int i=1;i<=m;i++) cin>>e[i].u>>e[i].v>>e[i].w;
sort(e,e+m+1,cmp);
init(n);
for(int i=1;i<=m;i++)
{
if(merge(e[i].u,e[i].v))
{
c++;
sum+=e[i].w;
}
if(c==n-k) break;
}
if(sum==0) cout<<"No Answer";
else cout<<sum;
return 0;
}
```
by hanzhongtlx @ 2019-10-14 19:30:35