来源 : POJ 2349
题目:
The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.
Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.
Input
The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).
Output
For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.
Sample Input
1
2 4
0 100
0 300
0 600
150 750
Sample Output
212.13
题意:可以使用s+1个点之间无视距离连通,其他点连通的代价是距离,求连接全图的最长边的最小值。
思路:用kruskal做最小生成树,s个点可以无视距离,于是就记录每次选的边长,设选了k条边,那么第k-s条就是答案
代码:
#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
int pre[250100],x[1000],y[1000];
double g[1000];
struct node
{
int u,v;
double w;
}a[250100];
bool cmp(node x,node y)
{
return x.w<y.w;
}
int find(int x)
{
while (pre[x]!=x)
x=pre[x];
return x;
}
int main()
{
int T,s,p,n,k;
cin>>T;
while (T--)
{
n=0;
scanf("%d%d",&s,&p);
for (int i=1;i<=p;i++)
scanf("%d%d",&x[i],&y[i]);
for (int i=1;i<p;i++)
for (int j=i+1;j<=p;j++)
{
n++;
a[n].u=i;
a[n].v=j;
a[n].w=sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2));
}
sort(a+1,a+1+n,cmp);
for (int i=1;i<=p;i++) pre[i]=i;
k=0;
for (int i=1;i<=n;i++)
{
if (find(a[i].u)!=find(a[i].v))
{
pre[find(a[i].u)]=find(a[i].v);
g[k++]=a[i].w;
}
}
printf("%.2f\n",g[k-s]);
}
return 0;
}