http://acm.hdu.edu.cn/showproblem.php?pid=1875
最小生成树,由于是稠密图,prim:O(T*n^2),kruskal:O(T*n^2logn^2),kruskal好写,勉强可以过。
枚举任两点,符合条件时加边即可。
#include<bits/stdc++.h>
using namespace std;
#define dist2(i,j) ((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]))
int t,c,m,x[105],y[105],tot;
double ans;
int p[105];
struct Edge{
int u,v;
double w;
bool operator < (Edge x){
return w<x.w;
}
}edge[10005];
int findset(int x){return p[x]==x?x:p[x]=findset(p[x]);}
int main()
{
freopen("input.in","r",stdin);
scanf("%d",&t);
while(t--)
{
m=0;ans=0;tot=0;
scanf("%d",&c);
for(int i=1;i<=c;i++)scanf("%d%d",&x[i],&y[i]);
for(int i=1;i<=c;i++)for(int j=i+1;j<=c;j++)
{
if(dist2(i,j)>=100&&dist2(i,j)<=1000000)
{
edge[++m].u=i;
edge[m].v=j;
edge[m].w=sqrt(dist2(i,j));
}
}
sort(edge+1,edge+1+m);
for(int i=1;i<=c;i++)p[i]=i;
for(int i=1;i<=m&&tot<c-1;i++)
{
int x=findset(edge[i].u),y=findset(edge[i].v);
if(x!=y)
{
tot++;
p[x]=y;
ans+=edge[i].w;
}
}
if(tot<c-1)puts("oh!");
else printf("%.1f\n",ans*100);
}
return 0;
}