暢通工程再續
每組數據首先是一個整數C(C <= 100),代表小島的個數,接下來是C組座標,代表每個小島的座標,這些座標都是 0 <= x, y <= 1000的整數。
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct node{
int x,y;
double cost;
bool operator<(const node &q)const{
return cost<q.cost;
}
}a[5000];
int n,p[101],cot,q[101][2];
double ans;
int fid(int x)
{
return p[x]==x?x:fid(p[x]);
}
void join(int x,int y,int c)
{
int f1=fid(x);
int f2=fid(y);
if(f1!=f2)
{
cot--;
ans+=a[c].cost;
p[f1]=f2;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ans=0.0;
scanf("%d",&n);
cot=n-1;
for(int i=0;i<n;i++)
p[i]=i;
for(int i=0;i<n;i++)
scanf("%d %d",&q[i][0],&q[i][1]);
int k=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
int tmp=(q[i][0]-q[j][0])*(q[i][0]-q[j][0])+(q[i][1]-q[j][1])*(q[i][1]-q[j][1]);
if(tmp>=100&&tmp<=1000000)
{
a[k].x=i;
a[k].y=j;
a[k++].cost=sqrt((double)tmp);
}
}
sort(a,a+k);
for(int i=0;i<k;i++)
join(a[i].x,a[i].y,i);
if(cot)
printf("oh!\n");
else
printf("%.1lf\n",ans*100);
}
return 0;
}