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
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.
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 750Sample Output
212.13
题意:自己写的时候有点想偷懒直接去看别人的翻译,果然还是自己读题理解得比较到位呀Orz
题目描述的就是给你m和n,接下来有n个顶点,在这n个顶点的图中构造最小生成树,找出这个最小生成树中权值第n-m大的那条边的权值。(就是m个卫星收发器可以抵偿两点之间的通讯代价d,也就是使dis[]为0,所以我们让m个卫星收发器替代权值最大的m条边)
----------------------------------------------------
算了还是手动翻译一遍吧
国防部希望在北极的几个哨所建立通讯网络,这里有两个通讯工具可供使用,一个是卫星收发器,一个是无线电收发器。卫星收发器可以没有代价的使两个哨所之间建立联系但只有m个,无线电收发器有无限个但是联系两个哨所之间需要代价d。给出每个哨所的坐标(x,y)(x,y都是整数,但两点之间的距离就不一定了),求出这个代价d最大是多少。
解题思路:
最大代价就是最小生成树中权值最大的边的权值呗,有了m个卫星收发器可以代替头m个权值最大的边,那么我们要找的就是第n-m大的权值的边的权值(好绕Orz)
在初始的时候就应该注意下标,后面还要涉及到sort和输出,这里下标是从1开始的。
直接prim构造最小生成树,最后对树的权值的集合(都在dis数组里)排下序,直接输出要找的那条边的权值。
AC代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
double x,y;
} q[610];
double mp[610][610],dis[610];
int book[610],n,m;
void prim()
{
for(int i=1;i<=n;i++)
{
dis[i]=mp[1][i];
}
dis[1]=0;
book[1]=1;
for(int i=1;i<=n-1;i++)
{
int minn=INF,u;
for(int j=1;j<=n;j++)
{
if(dis[j]<minn&&book[j]==0)
{
minn=dis[j];
u=j;
}
}
book[u]=1;
for(int j=1;j<=n;j++)
{
if(mp[u][j]<dis[j]&&book[j]==0)
{
dis[j]=mp[u][j];
}
}
}
sort(dis+1,dis+n+1);
// printf("%.2f\n",dis[0]);
// for(int i=1;i<=n;i++)
// {
// printf("dis[%d]===%.2f\n",i,dis[i]);
// }
// printf("5=====%.2f\n",dis[5]);
// printf("\n");
printf("%.2f\n",dis[n-m+1]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&q[i].x,&q[i].y);
}
for(int i=1;i<=n;i++)
{
dis[i]=INF;
for(int j=1;j<=n;j++)
{
if(i==j) mp[i][j]=0;
else mp[i][j]=INF;
}
}
memset(book,0,sizeof(book));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
mp[i][j]=sqrt((q[i].x-q[j].x)*(q[i].x-q[j].x)+(q[i].y-q[j].y)*(q[i].y-q[j].y));
}
}
// for(int i=1;i<=n;i++)
// {
// for(int j=1;j<=n;j++)
// {
// printf("%.2f ",mp[i][j]);
// }
// printf("\n");
// }
prim();
}
return 0;
}