POJ-2349 Arctic Network (最小生成樹)

Arctic Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23716   Accepted: 7274

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

#include <cstdio>  
#include <cmath>  
#include <algorithm> 
using namespace std;
struct edge{
	int from, to, val;
	bool operator < (const edge& x) const{
		return val < x.val;
	}
}e[320005];
int p[800], x[800], y[800];
double dis[320004];
int findset(int x){
	return p[x] == x ? x : p[x] = findset(p[x]); 
}
int main(){
	int T;
	scanf("%d", &T);
	while(T--){
		int s, u, v, n;
		scanf("%d %d", &s, &n);
		for(int i = 1; i <= n; ++i){
			scanf("%d %d", &x[i], &y[i]);
		}
		int num = 0;
		for(int i = 1; i <= n; ++i){
			for(int j = i + 1; j <= n; ++j){
				e[++num].from = i;
				e[num].to = j;
				e[num].val = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
			}
		}
		for(int i = 1; i <= n; ++i){
			p[i] = i;
		}
		sort(e + 1, e + 1 + num);
		int tot = 0, ans = 0;
	    for(int i = 1; i <= num; ++i){  
	        u = findset(e[i].from);  
	        v = findset(e[i].to);  
	        if(u != v){
	        	p[u] = v;
	        	++tot;
	        	dis[tot] = sqrt(e[i].val);
	        	if(tot == n - 1){
	        		break;
	        	}
	        }
	    }   
	    reverse(dis + 1, dis + 1 + tot);
	    printf("%.2f\n", dis[s]); 
	}
}

/*
題意:
100個點,求最小生成樹,s個衛星,衛星可以代替一條邊。

思路:
最小生成樹,改變一下,把最小生成樹的所有邊存下,用衛星代替長的邊即可。
*/


發佈了165 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章