HDU 1301 Jungle Roads (Kruscal 最小生成树)

  文章作者:ktyanny 文章来源:ktyanny 转载请注明,谢谢合作。  

  ktyanny:ktyanny今天有点累了,话说今天下午的马哲课3节,第一节研究了一下prim算法,在纸上画了半天也没缓过神来怎么用程序实现,很囧,改天要是弄明白了再把思路贴上来纪念一下吧。由于没想清楚要怎么实现,痛苦中困惑好累中。第二节课终于决定呼呼大睡了,第三节课由于老师要点名,我的美梦就很无辜地被中断了,囧……

  这道题目的大概意思和拿到畅通工程实质上是一样的,找到最小生成树即可,题目太简单我就不多说了。 

  0MS  C++ 


/*
by ktyanny
2009.12.14
*/
#include  < stdio.h >
#include  < stdlib.h >
#include  < string .h >
#include  < math.h >
#include  < iostream >
using   namespace  std;

const   int  MAX  =   105 ;

typedef  struct
{
    
int  a, b;
}point;

point v[MAX];

typedef  struct
{
    
int  x, y;
    
int  w;
}edge;
const   int  MAXN  =   50005 ;
edge e[MAXN];
int  ans;

int  rank[MAXN];
int  pa[MAXN];

void  make_set( int  x)
{
    pa[x]  =  x;
    rank[x]  =   0 ;
}

int  find_set( int  x)
{
    
if (x  !=  pa[x])
        pa[x]  =  find_set(pa[x]);
    
return  pa[x];
}

/* 按秩合并x,y所在的集合 */
void  union_set( int  x,  int  y,  int  w)
{
    x  =  find_set(x);
    y  =  find_set(y);
    
if (x  ==  y) return  ;
    ans  +=  w;
    
if (rank[x]  >  rank[y]) /* 让rank比较高的作为父结点 */
    {
        pa[y]  =  x;
    }
    
else  
    {
        pa[x]  =  y;
        
if (rank[x]  ==  rank[y])
            rank[y] ++ ;
    }
}


int  cmp( const   void   * a,  const   void   * b)
{
    
return  ( ( * (edge  * )a).w  >  ( * (edge  * )b).w )  ?   1  :  - 1 ;
}

int  main()
{
    
int  n, i, j, jj, x, y, k, t;
    
char  ch1, ch2;
    
while (cin  >>  n  &&  n)
    {
        k  =  n - 1 ;
        j  =   0 ;
        
for (i  =   0 ; i  <  k; i ++ )
        {
            cin  >>  ch1  >>  t;
            x  =  ch1  -   65 ;
            
for (jj  =   0 ; jj  <  t; jj ++ )
            {
                e[j].x  =  x;
                cin  >>  ch2  >>  y;
                e[j].y  =  ch2  -   65 ;
                e[j].w  =  y;
                j ++ ;
            }
        }

        
for (i  =   0 ; i  <=   30 ; i ++ )
            make_set(i);

        qsort(e, j,  sizeof (e[ 0 ]), cmp);

        
/* Kruscal过程求最小生成树 */
        ans  =   0 ;
        
for (i  =   0 ; i  <  j; i ++ )
        {
            x  =  find_set(e[i].x);
            y  =  find_set(e[i].y);
            
if (x  !=  y)
                union_set(x, y, e[i].w);
        }
        printf( " %d\n " , ans);
    }
    
return   0 ;
}

转载于:https://www.cnblogs.com/ktyanny/archive/2009/12/14/1624101.html

猜你喜欢

转载自blog.csdn.net/weixin_33676492/article/details/93965840