poj 2349

今天开始图论的专题~

/*
poj_2349    最小生成树
这题其实最让我纠结的是卫星数。前面都是套模板的,然后记录树每两点之间的距离。
看到网上每个人都说去掉最长的s-1条边剩下最长的就是了。想了一阵子才明白。。

其实卫星数就是连通分支的个数,有多少个卫星,就可以有多少个连通分支。这样每去
掉一条边,我们就多了一个分支。那么我们就可以去掉s-1条最长边,得到s个连通分支。
(每个分支里有一个卫星)剩下的最长边则为所求了。
*/
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
struct dis
{
    int from,to;
    double value;
};
struct point
{
    int x,y;
}po[510];
double dist[1010];
priority_queue <dis> q;
int father[510],k;

double count( int x1,int x2,int y1,int y2 )
{
    return sqrt( double( (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) ) );
}

bool operator<( const dis &a,const dis &b )
{
    return a.value>b.value;
}

int find_set( int a )
{
    if( a!=father[a] )
    {
        father[a]=find_set( father[a] );
    }
    return father[a];
}

void union_set( int a,int b,double v )
{
    if( a==b )  return;
    father[b]=a;
    dist[k++]=v;
}

int main()
{
    int T,s,p,i,j;
    dis temp;
    scanf( "%d",&T );
    while( T-- )
    {
        memset( dist,0,sizeof(dist) );
        scanf( "%d%d",&s,&p );
        for( i=0;i<p;i++ )
            father[i]=i;
        for( i=0;i<p;i++ )
        {
            scanf( "%d%d",&po[i].x,&po[i].y );
        }
        for( i=0;i<p-1;i++ )
        {
            for( j=i+1;j<p;j++ )
            {
                temp.from=i;
                temp.to=j;
                temp.value=count( po[i].x,po[j].x,po[i].y,po[j].y );
                q.push( temp );
            }
        }
        k=0;
        while( !q.empty() )
        {
            temp=q.top();    q.pop();
            union_set(  find_set(temp.from),find_set(temp.to),temp.value );
        }
        sort( dist,dist+k );
        printf( "%.2f\n",dist[ k-s ] );
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值