Problem Description
Problem Analysis
Once you understand what the problem is asking, it isn’t that hard
The gist is that some coordinates are given and you need to find the Minimum Spanning Tree
But the output required is the value of the largest edge, and since satellites can also handle communication
you only need, when there are P points and S satellites,
to discard the S-1 largest edges out of the P-1 edges in total
So here I feel Kruskal is the better choice
And when you have taken
P-1-(S-1)==P-S edges, that edge is exactly the one required
and no further processing is needed
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;
}
|
Problem link: 【POJ】[2349]Arctic Network