POJ - 2349 Arctic Network

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<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
const int MAX = 505;
int Father[MAX];
int n,m,u;
struct Node
{
	int x,y;
}s[MAX];
struct node
{
	int x1,y1;
	double w;
}ss[MAX * MAX];
bool compare(node x,node y)
{
	return x.w < y.w;
}
int Find(int x)
{
	if(x != Father[x])
	{
		Father[x] = Find(Father[x]);
	}
	return Father[x];
}
void Kruskal()
{
	int cnt = 0;
	for(int i = 0 ; i < u ; i++)
	{
		if(Find(ss[i].x1) != Find(ss[i].y1))
		{
			Father[Find(ss[i].x1)] = Find(ss[i].y1);
			cnt++;
			if(cnt == m - 1 - (n - 1))
			{
				printf("%.2f\n",ss[i].w);
			}
		}
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i = 1 ; i <= m ; i++)
		{
			Father[i] = i;
			scanf("%d%d",&s[i].x,&s[i].y);
		}
		u = 0;
		for(int i = 1 ; i <= m ; i++)
		{
			for(int j = i + 1 ; j <= m ; j++)
			{
				ss[u].x1 = i;
				ss[u].y1 = j;
				ss[u++].w = sqrt((s[i].x - s[j].x) * (s[i].x - s[j].x) + (s[i].y - s[j].y) * (s[i].y - s[j].y));
			}
		}
		sort(ss,ss + u,compare);
		Kruskal();
	}
}


猜你喜欢

转载自blog.csdn.net/MySweetFamily/article/details/80081074