您的位置:首页 > 其它

文章标题 POJ 2349:Arctic Network ( 最小生成树Kruskal算法+并查集)

2016-08-18 16:17 471 查看

Arctic Network

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.

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个卫星,p个哨所,有卫星的两个哨所可以无视距离通讯,而没有卫星的只能通过电台通讯,且只能与距离小于等于D的哨所通讯,现在要求最小的D使得每个哨所可以通过直接或者间接的与其他哨所通讯。

分析:其实这就是一道最小生成树的题,如果没有卫星的作用,而答案就是最大的两个哨所的距离,但这里还有s个卫星,其作用就是能使哨所之间的距离相当于0,所以当有s个卫星时,就是可以减少s-1条边。所以,可以用kruskal算法,先将任意两个哨所的距离求出来,然后排序,这样只需要求出第p-1-(s-1)条边就行了。

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
struct point {//定义哨所坐标的结构体
int x,y;
};
point a[505];
struct node {//定义从第x个哨所到第y个哨所需要的距离d的结构体
int x,y;
double d;
//重载运算符,将距离从小到大排序
bool operator <(const node &t)const{
return d<t.d;
}
};
node dist[505*505];
int fa[505];//每个哨所的父节点
int find (int x){
int r=x;
while (r!=fa[r]) r=fa[r];
while (x!=fa[x]){
int f=fa[x];
fa[x]=r;
x=f;
}
return r;
}
void merge (int a,int b){
int f1=find(a),f2=find(b);
if (f1>f2){
fa[f1]=f2;
}
else fa[f2]=f1;
}
double dis (point a,point b){//求两个哨所之间的距离
double temp=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
return sqrt(temp);
}
int s,p;
int main ()
{
int t;
scanf ("%d",&t);
while (t--){
scanf ("%d%d",&s,&p);
for (int i=0;i<p;i++) fa[i]=i;
for (int i=0;i<p;i++){
scanf ("%d%d",&a[i].x,&a[i].y);
}
int k=0;
for (int i=0;i<p;i++){
for (int j=0;j<i;j++){
dist[k].x=i;
dist[k].y=j;
dist[k++].d=dis(a[i],a[j]);
}
}
sort(dist,dist+k);
int place=p-1-(s-1);
int cnt=0;
double ans=0;
for (int i=0;i<k;i++){
if (cnt==place) break;
//如果两个哨所有共同的父节点,说明加入该边会形环
if (find(dist[i].x)!=find(dist[i].y)){
cnt++;
merge(dist[i].x ,dist[i].y );//合并两个哨所
ans=dist[i].d;
}
}
printf ("%.2f\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息