Description
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.
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
Source
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
和上一题并没有什么差别,(伪)最小生成树~
用s颗卫星可以代替s-1条边,那么就kruskal一下再输出长度排序为第s-p条边的长度即可~
(还是不能记录加入的边数tot……还是不明白为什么……反正以后不用就是了……)
(不能反向求,否则记录的是较大边;最好循环到结束,否则有可能输出的边不需要加。)
s-p实际上是(s-1)-p+1~
坐标是整数没有必要用double记录。
kkk和cnt都要初始化为0啊啊啊啊!!!
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int n,p,s,fa[501],ans[501],w[501][3],cnt,kkk;
struct node{
int x,y;
double v;
}a[500001];
double cal(int x1,int y1,int x2,int y2)
{
return sqrt(((double)x1-(double)x2)*((double)x1-(double)x2)+((double)y1-(double)y2)*((double)y1-(double)y2));
}
int findd(int u)
{
return fa[u]==u ? u:fa[u]=findd(fa[u]);
}
bool cmp(node u,node v)
{
return u.v<v.v;
}
int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&s,&p);cnt=0;kkk=0;
for(int i=1;i<=p;i++) scanf("%d%d",&w[i][1],&w[i][2]);
for(int i=1;i<=p;i++) fa[i]=i;
for(int i=1;i<=p;i++)
for(int j=i+1;j<=p;j++)
{
a[++cnt].x=i;a[cnt].y=j;a[cnt].v=cal(w[i][1],w[i][2],w[j][1],w[j][2]);
a[++cnt].x=j;a[cnt].y=i;a[cnt].v=a[cnt-1].v;
}
sort(a+1,a+cnt+1,cmp);
int i;
for(i=1;i<=cnt;i++)
if(findd(a[i].x)!=findd(a[i].y))
{
fa[findd(a[i].x)]=findd(a[i].y);ans[++kkk]=i;
}
printf("%.2lf\n",a[ans[p-s]].v);
}
return 0;
}