【NOIP校内模拟】T2 飞越行星带(kruskal)

啥玩意儿啊 题都没读懂

飞船要飞过这个行星带 就必须穿过每个行星形成的瓶颈

于是我们把每个行星想象成一个点 形成的瓶颈就是与其他点相连的边

相当于一个最小生成树了 直到s t联通

当然 这样做有点难理解 还可以类似的二分+并查集做

#include<bits/stdc++.h>
#define N 805
#define eps 1e-6
using namespace std;
int n,father[N],s,t;
double L;
struct Edge
{
	int from,to;
	double val;
}edge[N*N];
struct Point
{
	double x,y;
}p[N];
int tot;
inline void addedge(int x,int y,double z)
{
	tot++;
	edge[tot].from=x; edge[tot].to=y; edge[tot].val=z;
}
inline int getfather(int x)
{
	if(father[x]==x)	return x;
	father[x]=getfather(father[x]);
	return father[x];
}
inline double dis(Point a,Point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline bool cmp(const Edge &a,const Edge &b)
{
	return a.val<b.val;
}
inline bool Kruskal()
{
	father[n+1]=n+1,father[n+2]=n+2;
	sort(edge+1,edge+tot+1,cmp);
	for(int i=1;i<=tot;i++)
	{
		int fx=getfather(edge[i].from); int fy=getfather(edge[i].to);
		if(fx==fy)	continue;
		father[fx]=fy;	//
		if(getfather(n+1)==getfather(n+2))
		{
			cout<<fixed<<setprecision(3)<<edge[i].val;
			exit(0);
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	cin>>n>>L;
	s=n+1,t=n+2;
	for(int i=1;i<=n;i++)
	{
		father[i]=i;
		cin>>p[i].x>>p[i].y;
		for(int j=1;j<i;j++)	addedge(i,j,dis(p[i],p[j]));
		addedge(i,t,L-p[i].y);
		addedge(i,s,p[i].y);
	}
	Kruskal();
	return 0;
}
posted @ 2018-11-02 15:48  Patrickpwq  阅读(198)  评论(0编辑  收藏  举报