/*
zoj_1203 最小生成树
模板题,水过
*/
#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,count;
way temp;
count=1;
while( scanf("%d",&n) && n )
{
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 );
}
if( count!=1 ) printf( "\n" );
printf( "Case #%d:\n",count );
printf( "The minimal distance is: %.2lf\n",sum );
count++;
}
return 0;
}
zoj 1203
最新推荐文章于 2018-06-25 15:56:18 发布