POJ 2349 Arctic Network Prim算法+处理一些小细节

Arctic Network

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 28510   Accepted: 8612

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

扫描二维码关注公众号,回复: 5624985 查看本文章

题意解读:

有两种通信方式:卫星电话和无线电,有卫星电话可以彼此通信,有无线电的可以在一定范围内通信,让你求的就是这个连通所有哨所的无线电的范围。即求最小生成树中第n大边(指得是 能使哨所连通的最小代价的这些边中找到第n大边)。

实现思路:用一个double数组存这些最小代价边,用sort排序,比如说:他有三颗卫星,那输出应该为a[k-3] (k为最下代价边的总数)

下面说说我这个工兵班长所踩过的雷:

The first line of input contains N, the number of test cases.

它第一行是这样说的

而我用了个while(~scanf(…………)

然后Time Limit Exceeded

以前我在做题时,好像也遇到过第一行给出测试的数目

但是我用while(~scanf(…………)也没tle

这次就是卡到这。

还有一个地方:

就是sort对double 类型的数字排序

int cmp(const void *a,const void *b)
{
	return *(double *)a>*(double *)b?1:-1;
}

就是这个,很常用

下面正式贴代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define range 505
#define inf 0x3f3f3f3f
int cmp(const void *a,const void *b)
{
	return *(double *)a>*(double *)b?1:-1;
}
struct node 
{
	double x,y;
}s[range];
double map[range][range];

double dis[range];
double Distance(node a,node b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Prim(int m,int n)
{
	int k=0;
	double a[range];
	int pos=0;
	
    int i,j;
    for(i=0;i<n;i++)
    {
    	dis[i]=map[pos][i];
	}
	dis[pos]=0;
	for(i=0;i<n-1;i++)
	{
		double min=inf;
		for(j=0;j<n;j++)
		{
			if(dis[j]&&min>dis[j])
			{
				min=dis[j];
				pos=j;
			}
		}
		a[k++]=min;
	
		dis[pos]=0;
		for(j=0;j<n;j++)
		{
			if(dis[j]&&dis[j]>map[pos][j])
			dis[j]=map[pos][j];
		}
	}
	
	qsort(a,k,sizeof(a[0]),cmp);
//	for(i=0;i<k;i++)
//	printf("%lf ",a[i]);
//	printf("\n");
	return a[k-m];
	
}
int main()
{
	int n,n1,m1,i,j;
	
	while(~scanf("%d",&n))
	{
		scanf("%d %d",&n1,&m1);
		for(i=0;i<m1;i++)
		{
			scanf("%lf %lf",&s[i].x,&s[i].y);
			
		}
		memset(map,inf,sizeof(map));
		
		memset(dis,inf,sizeof(dis));
		for(i=0;i<m1;i++)
		{
			for(j=i+1;j<m1;j++)
			map[i][j]=map[j][i]=Distance(s[i],s[j]);
		}
		
		printf("%.2f\n",Prim(n1,m1));
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_41325698/article/details/88362804