蒟蒻刚学oi1e-9s,求助
查看原帖
蒟蒻刚学oi1e-9s,求助
88686
Shirο楼主2020/10/5 15:22
#define par
#include<bits/stdc++.h>
const int maxn=1e5+5; 
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
int xx[maxn],yy[maxn],prt[maxn],n,m,cnt=0;
#ifndef par
	#define dis(i,j) ((xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]))
#endif
int dis(int i,int j)
{
	return (xx[i]-xx[j])*(xx[i]-xx[j])
		   +
		   (yy[i]-yy[j])*(yy[i]-yy[j]);
}
using namespace std;
class edge
{
	public:
		int x,y,len;
}e[maxn];
bool cmp(edge e1,edge e2){return e1.len<e2.len;}
void input()
{
	scanf("%d%d",&n,&m);
	rep(i,1,n)
		scanf("%d%d",&xx[i],&yy[i]);
	rep(i,1,n)rep(j,i+1,n)
	{
		if(i!=j)
			e[++cnt].x=i,
			e[cnt].y=j,
			e[cnt].len=dis((i),(j));
	}
}
void init(){rep(i,1,maxn)prt[i]=i;} 
int find(int x){if(prt[x]!=x)prt[x]=find(prt[x]);return prt[x];}
void merge(int x,int y){prt[find(x)]=find(y);}
void solve()
{
	int tmp=0,ans=0;
	sort(e+1,e+cnt+1,cmp);
	rep(i,1,cnt)
	{
		
		#define from e[i].x
		#define to   e[i].y
		#define l    e[i].len
		if(find(prt[from])!=find(prt[to]))
		{
			merge(from,to);
			ans+=l;++tmp;
			cout<<from<<' '<<to<<' '<<l<<' '<<endl;
		}
		#undef from
		#undef to
		#undef l
		if(tmp==n-1)break;
	}
	if(ans<m)
		printf("-1\n");
	else printf("%d\n",ans);

}
int main()
{
	init();
	input();
	solve();
}

为什么欧几里得距离求解是错误的

2020/10/5 15:22
加载中...