标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17056 Accepted Submission(s): 5313
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct island
{
int x;
int y;
};
struct path
{
int a;
int b;
double dis;
};
struct island num1[110];
struct path num2[10000];
int father[110];
double sum;
bool cmp(path a,path b)
{
if(a.dis<b.dis)
return 1;
return 0;
}
int find(int x)
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
void merge(struct path xx)
{
int x,y;
x=find(xx.a);
y=find(xx.b);
if(x!=y)
{
father[x]=y;
sum+=xx.dis;
}
}
int main()
{
int t,c,i,j,cnt,cnt1;
double dist;
scanf("%d",&t);
while(t--)
{
scanf("%d",&c);
for(i=0; i<c; i++)
father[i]=i;
for(i=0; i<c; i++)
scanf("%d%d",&num1[i].x,&num1[i].y);
for(i=0,cnt=0; i<c; i++)
for(j=i+1; j<c; j++)
{
dist=sqrt((double)(num1[j].x-num1[i].x)*(num1[j].x-num1[i].x)+(num1[j].y-num1[i].y)*(num1[j].y-num1[i].y));
if(dist>=10&&dist<=1000)
{
num2[cnt].dis=dist;
num2[cnt].a=i;
num2[cnt].b=j;
cnt++;
}
}
if(cnt>=2)
sort(num2,num2+cnt,cmp); //排序注意时间复杂度,用自己写的排序(选择排序)可能会超时
sum=0;
for(i=0; i<cnt; i++)
merge(num2[i]);
for(i=0,cnt1=0; i<c; i++)
if(father[i]==i)
cnt1++;
if(cnt1==1)
printf("%.1lf\n",sum*100);
else
printf("oh!\n");
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/4391728.html