首页 > 代码库 > uva_10369_mst
uva_10369_mst
Description
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
Output
Sample Input
12 40 1000 3000 600150 750
Sample Output
212.13
这道题就是说,MST后,求所有边中的第s大的边。
用一个ans[MAXN]存储加入的边。
刚开始把设置MAXN=150,结果一直wa了6次,我竟然一直没有发现。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=550;
const double INF=999999999.9;
struct Point
{
double x,y;
}point[MAXN];
bool vis[MAXN];
double cost[MAXN][MAXN];
double lowc[MAXN];
double ans[MAXN];
double get_dis(int i,int j)
{
return (point[i].x-point[j].x)*(point[i].x-point[j].x)+(point[i].y-point[j].y)*(point[i].y-point[j].y);
}
int prim(int s,int n)
{
memset(vis,false,sizeof(vis));
vis[0]=true;
int tot=0;
for(int i=1;i<n;i++)
lowc[i]=cost[0][i];
for(int i=1;i<n;i++)
{
double minc=INF;
int p=-1;
for(int j=0;j<n;j++)
{
if(!vis[j]&&lowc[j]<minc)
{
minc=lowc[j];
p=j;
}
}
ans[tot++]=minc;//加入边的同时把边加入ans
vis[p]=true;
for(int j=0;j<n;j++)
if(!vis[j]&&cost[p][j]<lowc[j])
lowc[j]=cost[p][j];
}
sort(ans,ans+tot);
return ans[tot-s];
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
int s,n;
scanf("%d%d",&s,&n);
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&point[i].x,&point[i].y);//double型输入用lf
}
for(int i=0;i<n;i++)
{
lowc[i]=INF;
for(int j=0;j<n;j++)
cost[i][j]=cost[j][i]=get_dis(i,j);
}
double ans=prim(s,n);
ans=sqrt(ans);
printf("%.2f\n",ans);//double 型输出用f,记得\n
}
return 0;
}
uva_10369_mst