HDU1875畅通工程再续
思路:
模板题。
代码:
#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
#define cl(x) memset(x,0,sizeof(x))
const int N=1e5+10;
const int mod=1e7+9;
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
struct point
{
int x,y;
}a[110];
struct edge
{
int u,v;
double w;
}b[N];
int fa[110],len;
int cmp(edge x,edge y)
{
return x.w<y.w;
}
int find(int x)
{
if(fa[x]!=x)
fa[x]=find(fa[x]);
return fa[x];
}
int judge(int x,int y)
{
int t1=find(x),t2=find(y);
if(t1!=t2)
{
fa[t1]=t2;
return 1;
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,i,j;
scanf("%d",&n);
len=0;
for(i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
for(i=1;i<=n;i++)
fa[i]=i;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
double dis=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
if(dis>=10 && dis<=1000)
{
b[++len].u=i;
b[len].v=j;
b[len].w=dis;
}
}
sort(b+1,b+len+1,cmp);
int count=0;
double ans=0;
for(i=1;i<=len;i++)
{
if(judge(b[i].u,b[i].v))
{
count++;
ans+=b[i].w;
}
if(count==n-1)
break;
}
ans*=100;
if(count==n-1)
printf("%.1lf\n",ans);
else
printf("oh!\n");
}
return 0;
}