POJ - 2349 Arctic Network 最小生成树

GDUT 2020寒假训练 图论 E

原题链接

题目

原题截图

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.

样例
input
1
2 4
0 100
0 300
0 600
150 750
output
212.13

题目大意

给出n个卫星, m个哨站,n个卫星可以分配给哨站,每一个拥有卫星的哨站可以通过卫星频道和其他哨站向连接,否者就需要通过雷达相连接,但是雷达能大覆盖的距离是D,让你求这个最小的D

思路

最小生成树kruskal
首先,题目给出的两种通讯方法,一个是所有基站都有的雷达通信,还有就是给出的s个卫星通信。雷达通信只能允许点与点之间的通信。那么,我们想,如果没有卫星通讯,那么n个点找出最小的雷达覆盖距离就是,这n个点的最小生成树中的最大边。也就是说,将雷达覆盖范围D设置为该最大边的时候,所有的基站都可以通信。但是如果有了一个卫星,那好,舍弃刚刚的那条最大边,也就是现在得到了两个联通图,将卫星分配给那个单点的基站,这样单点的基站也可以通讯到另一个联通图,而这时,雷达的覆盖范围仅需要设置为另一个连通图里的最大边。那么如果再有一个卫星呢,就再删除这个最大边,然后同样,雷达的覆盖范围就是原最小生成树(没有卫星的时候)的第3大边。
那么,考虑到kruskal的算法是贪心选取当前的最小边,我们可以在kruskal的时候记录已经加入到最小生成树的边的个数,当边数达到了n-s的时候,那么这条边,就位雷达的覆盖区域。

另外,由于给出的是点的坐标,在转换成无向图的时候要进行一次n^2的枚举建边过程。

再另外,
最小生成树的实现中有一步骤是对边权进行排序,因为本着好写的角度,用来优先队列,实际上可以用sort。但是后来又想,sort的时间复杂度好像更小一点,但是听说sort在排序时要进行大量的复制操作,所以对结构体排序的时候(特别是结构体的成员比较多)可能map这样的会更快一点。于是在想,map,priority_queue和sort,也就是sort的排序和红黑树以及堆在插入和读取的速度上哪个更快?
我太菜了,有时间再研究研究。
[转] C++的STL库,vector sort排序时间复杂度 及常见容器比较

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath> 
#include<memory.h>
#include<vector>
#include<queue>
using namespace std;
double point[505][2];
struct Edge{
	int x,y;
	double dis;
	bool operator < (const Edge &e)const
	{
		return dis>e.dis;
	}
	
};
Edge pre(int x,int y,double d)
{
	Edge e;
	e.x=x;
	e.y=y;
	e.dis=d;
	return e;
}
double CalcDis(int x,int y)
{
	return sqrt((point[x][0]-point[y][0])*(point[x][0]-point[y][0])+(point[x][1]-point[y][1])*(point[x][1]-point[y][1]));
}
int fa[505];
int getfa(int x)
{
	return fa[x]==x?x:fa[x]=getfa(fa[x]);
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int s,n;
		cin>>s>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>point[i][0]>>point[i][1];
		}
		/*¿ªÊ¼½¨±ß*/
		priority_queue<Edge>e;
		int cnt=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=i+1;j<=n;j++)
			{
				e.push( pre(i,j,CalcDis(i,j) ) );
				//cnt++;
			}
			fa[i]=i;
		}
		/*for(int i=0;i<cnt;i++)
		{
			cout<<e[i].x<<" "<<e[i].y<<" "<<e[i].dis<<endl;
		}*/
		Edge noww;
		double ans=0;
		cnt=0;
		while(!e.empty())
		{
			noww=e.top();
			e.pop();
			int fx,fy;
			fx=getfa(noww.x);
			fy=getfa(noww.y);
			//cout<<noww.x<<" "<<noww.y<<" "<<noww.dis<<endl;
			if(fx!=fy)
			{
				fa[fx]=fy;
				ans=noww.dis;
				cnt++;
			}
			if(cnt==n-s)
			{
				break;
			}
		}
		printf("%.2lf\n",ans);
	}
	
	
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值