40 Pts,WA 求助!
查看原帖
40 Pts,WA 求助!
545986
Jerrycyx楼主2022/7/2 20:16

RT,讨论版里有很多类似的情况,但都不是我这种。

#include<cstdio>
#include<cmath>
#include<algorithm>

#define N 505
#define M N*N*2
#define K 105

using namespace std;

int n,m,k;
double ans=0;

struct Valiant{
	int x,y;
}P[N];
double dist(int a,int b)
{
	int x1=P[a].x,y1=P[a].y;
	int x2=P[b].x,y2=P[b].y;
	return (double)sqrt((double)(x1-x2)*(x1-x2)+(double)(y1-y2)*(y1-y2));
}

struct Allan{
	int from,to;
	int next;
	double val;
}edge[M],tree[N];
int edge_cnt=0,tree_cnt=0;
int head[N],tree_head[N];
void Add_edge(int from,int to,double value)
{
	edge_cnt++;
	edge[edge_cnt].from=from;
	edge[edge_cnt].to=to;
	edge[edge_cnt].val=value;
	edge[edge_cnt].next=head[from];
	head[from]=edge_cnt;
	return;
}
void Add_tree(int from,int to,double value)
{
	tree_cnt++;
	tree[tree_cnt].from=from;
	tree[tree_cnt].to=to;
	tree[tree_cnt].val=value;
	tree[tree_cnt].next=tree_head[from];
	tree_head[from]=tree_cnt;
	return;
}

int Father[N];
void Union_init()
{
	for(int i=1;i<=n;i++)
		Father[i]=i;
	return;
}
int Union_get(int x)
{
	if(Father[x]==x) return x;
	return Father[x]=Union_get(Father[x]);
}
void Union_merge(int x,int y)
{
	Father[Union_get(y)]=Union_get(y);
	return;
}

bool cmp(Allan x,Allan y)
{
	return x.val<y.val;
}
void Kruskal()
{
	sort(edge+1,edge+m+1,cmp);
	Union_init();
	for(int i=1;i<=m;i++)
	{
		int x=Union_get(edge[i].from);
		int y=Union_get(edge[i].to);
		if(x==y) continue;
		Father[x]=y;
		Add_tree(x,y,dist(x,y));
	}
	return;
}

bool cmp2(Allan x,Allan y)
{
	return x.val>y.val;
}
void Get_ans()
{
	sort(tree+1,tree+tree_cnt+1,cmp2);
	ans=tree[k].val;
	return;
}

int main()
{
	scanf("%d%d",&k,&n);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&P[i].x,&P[i].y);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			if(i==j) continue;
			Add_edge(i,j,dist(i,j));
			Add_edge(j,i,dist(i,j));
		}
	m=edge_cnt;
	Kruskal();
	Get_ans();
	printf("%.2lf\n",ans);
	return 0;
}

虽然学术版里的帖子被回复的概率很小,但还是要试一试。

2022/7/2 20:16
加载中...