Kruskal60分求助
查看原帖
Kruskal60分求助
722468
MrJC_Pandingding楼主2023/3/26 11:54
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int x,y;
};
struct edge
{
	int u,v;
	double w;
	friend bool operator<(const edge &x,const edge &y)
	{
		return x.w<y.w;
	}
	friend bool operator>(const edge &x,const edge &y)
	{
		return y<x;
	}
};
int cnt,f[1010],i,j,m,n,x,y;
double sumn;
node nd[1010];
edge e;
priority_queue<edge,vector<edge>,greater<edge> >pq;
double wy(node p,node q)
{
	return sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
}
int getf(int x)
{
	if(f[x]==x)
		return x;
	return f[x]=getf(f[x]);
}
int main()
{
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;++i)
	{
		scanf("%d%d",&x,&y);
		f[i]=i;
		nd[i].x=x;
		nd[i].y=y;
	}
	for(i=1;i<=n;++i)
	{
		for(j=1;j<=n;++j)
		{
			if(i!=j)
			{
				e.u=i;
				e.v=j;
				e.w=wy(nd[i],nd[j]);
				pq.push(e);
			}
		}
	}
	for(i=1;i<=m;++i)
	{
		scanf("%d%d",&x,&y);
		if(getf(x)!=getf(y))
		{
			cnt++;
			f[y]=f[getf(y)]=f[getf(x)];
		}
	}
	while(pq.size()&&cnt<=n-2)
	{
		e=pq.top();
		pq.pop();
		if(getf(e.u)!=getf(e.v))
		{
			cnt++;
			f[e.v]=f[getf(e.v)]=f[getf(e.u)];
			sumn+=e.w;
		}
	}
	if(cnt==n-1)
		printf("%.2f",sumn);
	return 0;
}

结果

哪位大佬帮帮我,悬赏关注。

2023/3/26 11:54
加载中...