poj 2349 Arctic Network(prime)

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25165   Accepted: 7751

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

题意:国防部(DND)要用无线网络连接北部几个哨所。有两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫星频道。

任何两个有卫星信道的哨所可以通过卫星进行通信,而不管他们的位置。同时,当两个哨所之间的距离不超过D时可以通过无线电通讯,D取决于对收发器的功率。功率越大,D也越大,但成本更高。出于采购和维修的方便,所有哨所的收发器必须是相同的;也就是说,D值对每一个哨所相同。

你的任务是确定收发器的D的最小值。每对哨所间至少要有一条通信线路(直接或间接)。

思路:有 m 个哨所需要m-1条边连接,s颗卫星可以代替s-1条边,用卫星代替最长的边

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define N 510

const double M=1e12;//定义一个大值
int p,s;

struct dot //哨站结点
{
	double x,y;
}d[N];

double map[N][N];
double rank[N],dst[N];//rank放最小生成树的各边长,dst放各点到MST的最近距离
int vis[N];

double dist(int i,int j)//求两点距离
{
	double x,y;
	x=d[i].x-d[j].x;
	y=d[i].y-d[j].y;

	return sqrt(x*x+y*y);
}

void init()
{
	int i,j;

	memset(map,0,sizeof(M));
	for (i=0;i<p;i++)//初始化图
	{
		for (j=0;j<p;j++)
		{
			if (i==j)
			{
				map[i][j]=0;
			}

			else
			{
				map[i][j]=map[j][i]=dist(i,j);
			}
		}
	}

	memset(vis,0,sizeof(vis));
	memset(dst,0,sizeof(dst));
}

//bool cmp(int i,int j)
//{
//	if (rank[i]<rank[j])
//	{
//		return true;
//	}
//	return false;
//}

void findans()
{
	int i,j;
	sort(rank,rank+p-1);//按增序排列,注意排序范围为rank+p-1,因为MST只有p-1条边
	printf("%.2f\n",rank[p-s-1]);// (p-1)-(s-1)-1,因为序号从0开始 

//	for (i=0;i<p;i++)
//	{
//		for (j=0;j<p;j++)
//		{
//			printf("%10.2f",map[i][j]);
//		}
//		printf("\n");
//	}
//	printf("\n");
//	for (i=0;i<p-1;i++)
//	{
//		printf("%10.2f",rank[i]);
//	}
}

void prime()
{
	int cnt=0,k,j,point,i;
	double min;

	vis[0]=1;//把0点放入MST
	for (i=0;i<p;i++)//初始化dst
	{
		dst[i]=map[i][0];
	}

	for (i=1;i<p;i++)//找距MST最近的点
	{
		min=M;
		for (j=0;j<p;j++)
		{
			if (vis[j]==0&&min>dst[j])
			{
				min=dst[j];
				point=j;
			}
		}

//		if (min==M)
//		{
//			break;
//		}

		vis[point]=1;
		rank[cnt++]=min;

		for (k=0;k<p;k++)//更新各点到MST的最小距离
		{
			if (vis[k]==0&&dst[k]>map[k][point])
			{
				dst[k]=map[k][point];
			}
		}
	}
	findans();
}

int main()
{
	int i,n,j;
	double x,y;

	scanf("%d",&n);
	for (i=0;i<n;i++)
	{
		scanf("%d%d",&s,&p);

		for (j=0;j<p;j++)
		{
			scanf("%lf%lf",&d[j].x,&d[j].y);
		}

		init();
		prime();
	}

	return 0;
}

原文地址:https://www.cnblogs.com/hemeiwolong/p/8996192.html

时间: 2024-10-03 21:57:47

poj 2349 Arctic Network(prime)的相关文章

POJ 2349 Arctic Network (最小生成树)

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 a

poj 2349 Arctic Network (prim算法)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10525   Accepted: 3470 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

2349 Arctic Network(中文版)

试题描述: 国防部希望通过无线网络连接几个北方前哨基地. 在建立网络时将使用两种不同的通信技术:每个前哨基站都将拥有无线电收发器,另外还有一些前哨卫星通道. 任何带卫星频道的两个前哨都可以通过卫星进行通信,无论其位置如何. 否则,只有两个前哨基站之间的距离不超过D,才能通过无线电通信,这取决于收发器的功  率.  更高的功率产生更高的D,但成本更高. 由于采购和维护的考虑,前哨收发器必须相同; 也就是说,D的价值对于每对前哨都是一样的. 您的工作是确定收发器所需的最小D. 在每对前哨之间必须至少

POJ 2349 Arctic Network 最小生成树题解

本题也是使用Prime和Kruskal都可以的最小生成树的题解. 本题一点新意就是:需要除去最大的S-1个距离,因为可以使用卫星覆盖这些距离. 技巧:建图建有向图,速度快点,不用计算两边. 这里使用Prime,因为是稠密图. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <al

poj 1459 Power Network (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

poj 2349 Arctic Network MST/二分答案

poj 2349 Arctic Network 题目传送 Sol: 方法一: 贪心的想,发现n个点只需要n-1条边即可,求MST即可,再把MST中最大的m-1条边去掉,第m大就是答案. code: #include<string> #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define IL inline #define RG register

POJ 2349 Arctic Network (最小生成树第K大(小)边)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13108   Accepted: 4256 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 2349 Arctic Network

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18447   Accepted: 5829 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 2349 Arctic Network(最下生成树+求第k大边)

题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两个前哨之间的距离不超过D,才能通过无线电进行通信.求出能使所有前哨都能直接或间接通信的最小的D. 解题思路:题目要求使所有前哨都能直接或间接通信,那么相当于使n个点相连,至少需要n-1条边.可以将n个点分为s个团,每个团内部时无限通信,团与团之间通过卫星通信.那么就相当于用s个卫星装置建立s-1条边