POJ - 2349 Arctic Network

Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 22506
Accepted: 6952

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

Source

Waterloo local 2002.09.28

题意:
有m个哨所,n个卫星。每个卫星可以减少一条边相连来保持通信。问借此形成互通的图需要相连的最大权值是多少

思路:

用kruscal算法把相连的边从大到小排序,然后每循环一次判断是否为第m-n个边,如果是则输出此时的权值,否则继续循环,卫星数加一,直到结束。
当卫星数等于m-n时,即若有1个卫星,4条边要连,我们可以把最大的用卫星来通信,其他的相连,则输出的答案就为第二大的边权。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

int n,m,num,sx;
struct data1{
	int x,y;
}p[250000];

struct data{
	int x,y;
	double c;
}edge[250000];
int f[5005];

void fun(){
	num=0;
	for(int i=1;i<=m;i++){
		for(int j=i+1;j<=m;j++){
			double l=sqrt(1.0*(p[i].x-p[j].x)*(p[i].x-p[j].x)+1.0*(p[i].y-p[j].y)*(p[i].y-p[j].y));
			edge[num].x=i;
			edge[num].y=j;
			edge[num++].c=l;
		}
	}
}

void pre(){
	for(int i=0;i<=m;i++)f[i]=i;
}

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

bool cmp(data a,data b){
	return a.c<b.c;
}

void kruscal(){
	int k=0;
	sort(edge,edge+num,cmp);
	for(int i=0;i<num;i++){
		int x=find(edge[i].x);
		int y=find(edge[i].y);
		
		if(x!=y){
			f[x]=y;
			k++;
		}
		if(k==m-n){
			printf("%.2lf\n",edge[i].c);
			break;
		}
	}
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		pre();
		for(int i=1;i<=m;i++){
			scanf("%d%d",&p[i].x,&p[i].y);
		}
		fun();
		kruscal();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值