寒假训练图论E

题目
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.
题目大意
有P个前哨站,给出每个前哨站的坐标。每个前哨站有无线电收发器,一些前哨站有一个卫星频道(共S个)。任意两个前哨站之间可以无线电通讯(有距离限制不超过D,前哨站的收发器都是相同的,所以每对前哨站的D值是相同的),只有两个前哨站均有卫星频道,他们之间才能通过卫星通讯(无论他们的距离有多远)。问使得任意两个前哨站均有至少一条通讯路径(直接或间接),求最小的D值。
解题思路

   由于题目要求任意两个前哨站均有至少一条通讯路径(直接或间接)
   显然D的值要从任意两个前哨站之间的距离找。
   S个卫星频道能解决S个点的通讯问题,即解决了S-1条边。

所以题目就转化成求最小生成树种第S大的边的权值。
关于边的最小生成树问题用kruscal算法,记录选了的边,然后输出第S大的边的权值。
代码

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
int father[10101];
double b[252525];
struct data
{
	int x,y;
	double v;
}c[252525];
struct wahaha
{
	double x,y;
}a[1010];
int get_father(int x)
{
	if (father[x]==x) return x;
	father[x]=get_father(father[x]);
	return father[x];
}
bool cmp(data x,data y)
{
	return (x.v<y.v);
}
int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		int s,p,m=0;
		scanf("%d%d",&s,&p);
		for (int i=1;i<=p;i++)
		  scanf("%lf%lf",&a[i].x,&a[i].y);
		for (int i=1;i<=p;i++) father[i]=i;  
		for (int i=1;i<=p;i++)
		  for (int j=i+1;j<=p;j++)
		    {
		      double x=(a[i].x-a[j].x);
		      double y=(a[i].y-a[j].y);
		      c[++m].v=sqrt(x*x+y*y);
			  c[m].x=i;
			  c[m].y=j;  	 
	        }
	     sort(c+1,c+m+1,cmp);
	     int num=0;
		 for (int i=1;i<=m;i++)
		   {
		   	  int xx=get_father(c[i].x);
		   	  int yy=get_father(c[i].y);
		   	  if (xx==yy) continue;
		   	  father[xx]=yy;
		   	  num++;
		   	  b[num]=c[i].v;
		   }  
		 printf("%.2f\n",b[num-(s-1)]);   
	}
}
发布了41 篇原创文章 · 获赞 0 · 访问量 595

猜你喜欢

转载自blog.csdn.net/weixin_45723759/article/details/103998647