【POJ】[2349]Arctic Network

文章字数:389

问题描述

问题分析

理解题意之后也并不算太难
大意是给了一些坐标来求最小生成树
然而让输出的是最大边的值,而因为还有卫星可供通讯
所以只需要当有P个点S个卫星时
共P-1条边里舍去S-1条大的边

所以这里我感觉用Kruskal更好一些
而且当取了
P-1-(S-1)==P-S条边时,这一条边便是所要求边
不需要进行后续操作了

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;

struct edge {
	int u,v;
	double dis;
} e[250200];

int par[520];
int ran[520];

int find(int m) {
	if(m==par[m])
		return m;
	else
		return par[m]=find(par[m]);
}
void unite(int x,int y) {
	x=find(x);
	y=find(y);
	if(x==y)
		return;
	if(ran[x]<ran[y])
		par[x]=y;
	else {
		par[y]=x;
		if(ran[x]==ran[y])
			ran[x]++;
	}
}
bool cmp(edge A,edge B) {
	return A.dis<B.dis;
}

struct xy {
	double x,y;
} x[520];
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int S,N;
		scanf("%d %d",&S,&N);
		for(int i=1; i<=N; i++) {
			par[i]=i;
			ran[i]=0;
		}
		for(int i=1; i<=N; i++) {
			scanf("%lf %lf",&x[i].x,&x[i].y);
		}
		int cnt=-1;
		for(int i=1; i<=N; i++) {
			for(int j=1; j<=N; j++) {
				if(i==j)
					continue;
				e[++cnt].u=i;
				e[cnt].v=j;
				e[cnt].dis=sqrt(pow(x[i].x-x[j].x,2)+pow(x[i].y-x[j].y,2));
			}
		}
		sort(e,e+cnt+1,cmp);
		double sum=0;
		int res=0;
		for(int i=0; i<=cnt; i++) {
			if(find(e[i].u)!=find(e[i].v)) {
				res++;
				if(res==N-S) {
					printf("%.2lf\n",e[i].dis);
					break;
				}
				unite(e[i].u,e[i].v);
			}
		}
	}
	return 0;
}

题目地址:【POJ】[2349]Arctic Network

该内容采用 CC BY-NC-SA 4.0 许可协议。

如果对您有帮助或存在意见建议,欢迎在下方评论交流。

加载中...