【POJ】2349 - Arctic Network(最小生成树的第n条大边)(好题)

Arctic Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15114   Accepted: 4832

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

G - Arctic Network
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

国防部(DND)要用无线网络连接北部几个哨所。两种不同的通信技术被用于建立网络:
每一个哨所有一个无线电收发器,一些哨所将有一个卫星频道。
任何两个有卫星信道的哨所可以通过卫星进行通信,而不管他们的位置。同时,当两个
哨所之间的距离不超过D时可以通过
无线电通讯,D取决于对收发器的功率。功率越大,D也越大,但成本更高。出于采购
和维修的方便,所有哨所的收发器必
须是相同的;那就是说,D值对每一个哨所相同。
 
你的任务是确定收发器的D的最小值。每对哨所间至少要有一条通信线路(直接或间接)。

Input

输入的第一行是测试数据的数量N。
每组测试数据的第一行包含卫星频道的数量S(1 < = S < = 100)和哨所的
数量P(S < P < = 500)。接下来的P行,给出以公
里为单位的每个哨所的坐标(x,y)( 坐标为0到10000之间的整数)。

Output

对于每组测试数据,输出一行,输出收发器的D的最小值。精确到小数点后两位。

Sample Input

1
2 4
0 100
0 300
0 600
150 750

Sample Output

212.13


这道题我现在的水平也不能给出一个合理的证明过程,但是计算的方法现在懂了,就是先按照克鲁斯塔尔的方法生成最小树,并用一个数组保存生成树时边的大小,s个卫星可以代替s-1条边,这样求次s-1大的边就行了。

代码如下:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int f[511];
struct node1
{
	int st,end;
	double cost;
}road[500*250];
struct node2
{
	double x,y;
}data[511];
int find(int x)
{
	if (x!=f[x])
		f[x]=find(f[x]);
	return f[x];
}
int join (int x,int y)
{
	int fx,fy;
	fx=find (x);
	fy=find (y);
	if (fx!=fy)
	{
		f[fx]=fy;
		return 1;
	}
	return 0;
}
bool cmp1(node1 a,node1 b)
{
	return a.cost<b.cost;
}
bool cmp2 (int a,int b)
{
	return a<b;
}
double dis(node2 a,node2 b)
{
	double ans;
	ans=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
	return ans;
}
int main()
{
	int n,s;
	double d[511];		//记录生成树的边数 
	int u;
	scanf ("%d",&u);
	while (u--)
	{
		scanf ("%d %d",&s,&n);
		for (int i=1;i<=n;i++)
			f[i]=i;		//别忘了初始化 
		for (int i=1;i<=n;i++)
			scanf ("%lf %lf",&data[i].x,&data[i].y);
		int num=-1;
		for (int i=1;i<n;i++)
		{
			for (int j=i+1;j<=n;j++)
			{
				num++;
				road[num].st=i;
				road[num].end=j;
				road[num].cost=dis(data[i],data[j]);
			}
		}
		sort (road,road+num+1,cmp1);		//这里注意num要 +1 
		int t=-1;
		for (int i=0;i<=num;i++)
		{
			if (join(road[i].st,road[i].end))
			{
				t++;
				d[t]=road[i].cost;
			}
		}
		sort (d,d+t+1,cmp2);
		printf ("%.2lf\n",d[n-1-s]);
	}
	return 0;
}


你可能感兴趣的:(【POJ】2349 - Arctic Network(最小生成树的第n条大边)(好题))