标签:
2 2 10 10 20 20 3 1 1 2 2 1000 1000
1414.2 oh!
将坐标处理成边,用kruskal算法取边。。。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define M 204
using namespace std;
struct node{
int a,b;
double v;
}edge[M*100];
double father[204],sum;
bool cmp(node a,node b)
{
return a.v<b.v;
}
void init(int x)
{
int i;
for(i=0;i<=x;i++)
father[i]=i;
}
int findroot(int x)
{
int r=x;
while(r!=father[r])
r=father[r];
int i=x,j;
while(i!=r)
{
j=father[i];
i=father[r];
i=j;
}
return r;
}
int join(int a, int b)
{
int fx=findroot(a);
int fy=findroot(b);
if(fx!=fy){father[fx]=fy;return 1;}
return 0;
}
int main()
{
int T,C,i,j,k,m;
double x[M],y[M];
scanf("%d",&T);
while(T--)
{
sum=0;
scanf("%d",&C);
init(C);
for(i=1;i<=C;i++)
{
scanf("%lf%lf",&x[i],&y[i]);
}
k=0;
for(i=1;i<=C;i++)
{
for(j=1;j<i;j++)
{
double bian;
bian=sqrt((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]));
if(bian>=10&&bian<=1000)
{
edge[k].a=i;edge[k].b=j;
edge[k].v=bian;
k++;
}
}
}
if(k==0){
printf("oh!\n");continue;
}
sort(edge,edge+k,cmp);
m=0;
j=0;
while(m<C&&j<k)
{
if(join(edge[j].a,edge[j].b
)){
sum=sum+edge[j].v;
m++;
}
j++;
}
if(m!=C-1)printf("oh!\n");
else printf("%.1f\n",sum*100);
}
return 0;
}
hdu1875 畅通工程再续(最小生成树kruskal算法)
标签:
原文地址:http://blog.csdn.net/aaaaacmer/article/details/45890521