您的位置:首页 > 其它

poj-2349 Arctic Network 最小生成树

2014-07-27 15:01 579 查看
题目链接

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1005;
const int Mod = 1000000007;
int s,n,m,c;
double ans;
struct Node
{
int x,y;
}point[maxn];
struct node
{
int u,v;
double w;
}edge[maxn*maxn];
int p[maxn];
bool cmp( node a,node b )
{
return a.w < b.w;
}
int find( int x )
{
return x == p[x]?x:p[x] = find( p[x] );
}
void merge( node s )
{
int x = find( s.u );
int y = find( s.v );
if( x != y )
{
p[x] = y;
c ++;
ans = s.w;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int cas;
scanf("%d",&cas);
while( cas -- )
{
scanf("%d%d",&s,&n);
ans = m = c = 0;
for( int i = 1; i <= n; i ++ )
{
scanf("%d%d",&point[i].x,&point[i].y);
}
for( int i = 1; i <= n; i ++ )
{
for( int j = i+1; j <= n; j ++ )
{
edge[m].u = i;  edge[m].v = j;
edge[m++].w = sqrt( (( point[i].x - point[j].x )*( point[i].x - point[j].x ) + ( point[i].y - point[j].y )*( point[i].y - point[j].y ))*1.0 );
}
}
sort( edge,edge+m,cmp );
for( int i = 0; i <= n; i ++ )
p[i] = i;
for( int i = 0; i < m; i ++ ){
merge( edge[i] );
if( c == n - s )
break;
}
printf("%.2lf\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: