题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1875
C语言源码:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define maxsize 10000
typedef struct edge
{
int a,b;
double length;
}edge;
edge E[maxsize];
int cmp(const void *a,const void *b)
{
edge *aa=(edge *)a;
edge *bb=(edge *)b;
return aa->length>bb->length?1:-1;
}
int T[110];
int findroot(int x)
{
int temp;
if(T[x]==-1)
return x;
else
{
temp=findroot(T[x]);
T[x]=temp;
return temp;
}
}
int main()
{
int cas,n,a[110][2],i,j,top,num,c,d,y;
double x,sum;
while(scanf("%d",&cas)!=EOF)
{
while(cas--)
{
top=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d %d",&a[i][0],&a[i][1]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
x=sqrt((a[i][0]-a[j][0])*(a[i][0]-a[j][0])+(a[i][1]-a[j][1])*(a[i][1]-a[j][1]));
y=(a[i][0]-a[j][0])*(a[i][0]-a[j][0])+(a[i][1]-a[j][1])*(a[i][1]-a[j][1]);
if(y>=100&&y<=1000*1000)
{
E[top].a=i;
E[top].b=j;
E[top++].length=x;
}
}
}
qsort(E,top,sizeof(E[0]),cmp);
for(i=0;i<n;i++)
T[i]=-1;
sum=0;
num=0;
for(i=0;i<top;i++)
{
c=findroot(E[i].a);
d=findroot(E[i].b);
if(c!=d)
{
T[c]=d;
sum+=E[i].length;
num++;
}
}
if(num==n-1)
printf("%.1lf\n",sum*100);
else
printf("oh!\n");
}
}
}