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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章