UVa - 10369 - Arctic Network ( 最小生成树 Kruscal )

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.

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). 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

题目大意:国防部要让 p 个地方实现通信。实现通信的方式有2种,第一种是在该地方连接卫星(可以实现任意距离的通信),第二种是在两地之间建立无线电通信,建造成本和距离D成正比。现在有 s 个卫星,如果要实现 p 个地方的通信(直接或间接)。让D尽可能的小,求出最大的D。题目输入给出每个地方的坐标。

题目思路:构图后求出MST,那么较大的边尽可能都用卫星通信,剩下的最大的边就是要求的D。


#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 505
using namespace std;

double x[N],y[N];
int f[N];
int num,cnt,s,p;

struct  E{
	int a,b;
	double v;
}e[N*N];

bool cmp(E e1,E e2){
	return e1.v<e2.v;
}

int find(int x){
	return f[x] == x ? x : f[x] = find(f[x]);
}

void Kruscal(){
	sort(e,e+num,cmp);
	
	for(int i=0 ;i<num ;i++){
	
		int fa = find(e[i].a);
		int fb = find(e[i].b);
	
		if(fa!=fb){
			f[fa] = fb;
			cnt++;
		}
		if(cnt == p-s){
			printf("%.2f\n",e[i].v);
			break;
		}
	}
	
}


void init(){
	scanf("%d%d",&s,&p);
	
	for(int i=0 ;i<N ;i++){
		f[i] = i;
	}
	
	for(int i=0 ;i<p ;i++){
		scanf("%lf",&x[i]);
		scanf("%lf",&y[i]);
	}
	
	for(int i=0 ;i<p ;i++){
		for(int j=i+1 ;j<p ;j++){
			e[num].a = i;
			e[num].b = j;
			e[num++].v = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
		}
	}
}
int t;
int main(){
	scanf("%d",&t);
		while(t--){
			num = 0;
			cnt = 0;
			init();
			Kruscal();			
		}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值