# 畅通工程再续/HDOJ 1875

## 代码：

#include <stdio.h>
#include <cstring>
#include <queue>
#include <math.h>
#include <algorithm>
using namespace std;
struct CC
{
double dis;
int a,b;
}edge[10000];
int x[110],y[110],fa[110],s,n,T,l,l1;
double sum,dis1;

bool cmp(CC x,CC y)
{
return x.dis<y.dis;
}

int getfather(int x)
{
if (fa[x]==x) return fa[x];
fa[x]=getfather(fa[x]);
return fa[x];
}

int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
l=l1=0;
for (int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
{
dis1=sqrt((double)((y[j]-y[i])*(y[j]-y[i])+(x[j]-x[i])*(x[j]-x[i])));
if (dis1>=10 && dis1<=1000)
{
l++;
edge[l].a=i;edge[l].b=j;edge[l].dis=sqrt((double)((y[j]-y[i])*(y[j]-y[i])+(x[j]-x[i])*(x[j]-x[i])));
}
}
sort(edge+1,edge+l+1,cmp);
for (int i=1;i<=n;i++) fa[i]=i;
sum=0;
for (int i=1;i<=l;i++)
{
int fx=getfather(edge[i].a);
int fy=getfather(edge[i].b);
if (fx!=fy)
{
fa[fy]=fx;
l1++;
sum+=edge[i].dis;
}
}
if (l1==n-1) printf("%.1f\n",sum*100); else printf("oh!\n");
}
return 0;
}


©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客