/*
zoj_1872 最小生成树
模板题,水过
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
struct point
{
double x,y;
int id;
}p[110];
struct way
{
int from,to;
double len;
};
way w[10010];
priority_queue <way>pq;
int father[110];
double sum;
bool operator<( const way&a,const way&b )
{
return a.len>b.len;
}
int find_set( int a )
{
if( father[a]!=a )
{
father[a]=find_set( father[a] );
}
return father[a];
}
void union_set( int a,int b,double c )
{
if( a==b ) return;
father[b]=a;
sum+=c;
}
int main()
{
int n,i,j;
way temp;
while( scanf("%d",&n)!=EOF )
{
for( i=0;i<n;i++ )
{
scanf( "%lf%lf",&p[i].x,&p[i].y );
p[i].id=i ;
father[i]=i;
}
for( i=0;i<n;i++ )
{
for( j=i+1;j<n;j++ )
{
temp.from=p[i].id;
temp.to=p[j].id;
temp.len=sqrt( ( p[i].x-p[j].x )*( p[i].x-p[j].x )+( p[i].y-p[j].y )*( p[i].y-p[j].y ) );
pq.push( temp );
}
}
sum=0.0;
while( !pq.empty() )
{
temp=pq.top(); pq.pop();
union_set( find_set(temp.from),find_set(temp.to),temp.len );
}
printf( "%.2lf\n",sum );
}
return 0;
}
zoj 1872
最新推荐文章于 2018-06-22 23:07:19 发布