HDU_1875_mst
Description
Input
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
Output
Sample Input
Sample Output
#include<cstdio>
#include<cstring>
#include<cmath>
const double INF=999999999.9;
const int MAXN=103;
struct Pointer
{
double x;
double y;
}pointer[MAXN];
bool vis[MAXN];
double cost[MAXN][MAXN];
double lowc[MAXN];
double dis(int i,int j)
{
return sqrt((pointer[i].x-pointer[j].x)*(pointer[i].x-pointer[j].x)+\
(pointer[i].y-pointer[j].y)*(pointer[i].y-pointer[j].y));
//可以等到最后才开方
}
double prim(int n)
{
memset(vis,false,sizeof(vis));
vis[1]=true;
double ans=0.0;
for(int i=2;i<=n;i++)
lowc[i]=cost[1][i];
for(int i=2;i<=n;i++)
{
double minc=INF;
int p=-1;
for(int j=1;j<=n;j++)
if(!vis[j]&&lowc[j]<minc)
{
minc=lowc[j];
p=j;
}
if(minc==INF)
return -1;
ans+=minc;
vis[p]=true;
for(int j=1;j<=n;j++)
if(!vis[j]&&cost[p][j]<lowc[j])
lowc[j]=cost[p][j];
}
return ans;
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf%lf",&pointer[i].x,&pointer[i].y);//double型输入用lf
for(int i=1;i<=n;i++)
{
lowc[i]=INF;
for(int j=1;j<=n;j++)
{
double temp=dis(i,j);
if(temp>=10.0&&temp<=1000.0)
cost[i][j]=temp;
else
cost[i][j]=INF;
}
}
double ans=prim(n);
if(ans==-1)
printf("oh!\n");
else
printf("%.1f\n",ans*100);//double 型输出用f
}
return 0;
}