题目地址:​​点击打开链接​

题意:给你p个基站,s个卫星,卫星不管任何距离都可以连接,无线连接的距离越长,花费越高,求把n个基站连在一起并且花费最小时,无线的最短距离为多少

思路:我的AC代码用C++投会A,用G++投会wrong,我也不知道为啥,组成最小生成树的前s-1条最大边用卫星(至于为啥s个卫星只能替代s-1条边,我也不知道),剩下的用无线

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

using namespace std;

int s,p;

struct city
{
int x,y;
}a[510];


double getdist(struct city a,struct city b)
{
return sqrt(1.0 * (a.x - b.x)*(a.x - b.x) + 1.0 * (a.y - b.y)*(a.y - b.y));//这里用G++投不乘以1.0不会报错,但用C++投不乘以1.0会报错,还是要乘以1.0的,因为sqrt的参数没有int类型的
}

bool cmp(double a,double b)
{
return a > b;
}

double map1[510][510];
int visit[510];
double lowdist[510];
double edge[510];

void Prim()
{
int i,j,k;
int l = 0;
memset(visit,0,sizeof(visit));
for(i=1; i<=p; i++)
{
lowdist[i] = map1[1][i];
}
visit[1] = 1;
for(i=1; i<p; i++)
{
double min2 = 10000000;//注意是double
for(j=1; j<=p; j++)
{
if(!visit[j] && lowdist[j] < min2)
{
min2 = lowdist[j];
k = j;
}
}
edge[l++] = lowdist[k];
visit[k] = 1;
for(j=1; j<=p; j++)
{
if(!visit[j] && map1[k][j] < lowdist[j])
{
lowdist[j] = map1[k][j];
}
}
}
sort(edge,edge+l,cmp);
printf("%.2lf\n",edge[s-1]);//标号是从0开始的,所以输出第s条边
}

int main()
{
int t;
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&p);
for(i=1; i<=p; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
for(i=1; i<=p; i++)
{
for(j=i+1; j<=p; j++)
{
map1[i][j] = map1[j][i] = getdist(a[i],a[j]);
}
}
Prim();
}
return 0;
}