HDU 1875 adjourned to expedite project (Kruscal minimum spanning tree)

  Article author: ktyanny  Source: ktyanny  reproduced please specify, thank you.  

   ktyanny: Well, the Chinese title, then the title describes not say more. With a look that is thought to solve the minimum spanning tree. The subject did not 1Y it is a pity that is beginning to do with a float, WA, and the type to double on AC, and a little embarrassing ......

 

   312MS C++

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

const   int  MAX  =   105 ;

typedef 
struct
{
    
int  x, y;
    
double  w;
}edge;
const   int  MAXN  =   50005 ;
Edge E [MAXN];
Double  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]; } / * press rank combined x set y where, * / void  union_set ( int  x,  int  Y,  Double  W) {  




    


    






    X 
=  find_set (X);
    Y 
=  find_set (Y);
    
IF (X  ==  Y) return  ;
    ANS 
+ =  W;
    
IF (rank [X]  >  rank [Y]) / * make higher rank as a parent node point * /
    {
        PA [Y] 
=  X;
    }
    
the 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 ;
}

typedef 
struct
{
    
double  xx, yy;
}point;

point P[
105 ];
int  main()
{
    
int  n, i, j, jj,k, x, y, t, m;
    
char  ch1, ch2;
    cin 
>>  m;    
    
while (m -- )
    {
        cin 
>>  n;
        j 
=   0 ;
        
for (i  =   1 ; i  <=  n; i ++ )
            cin 
>>  P[i].xx  >>  P[i].yy;

        
/* 处理图的边集 */
        
double  temp;
        
for (i  =   1 ; i  <=  n; i ++ )
        {
            
for (k  =  i; k  <=  n; k ++ )
            {
                temp 
=  sqrt((P[i].xx - P[k].xx) * (P[i].xx - P[k].xx)  +  (P[i].yy - P[k].yy) * (P[i].yy - P[k].yy) );
                
if (temp  <   10   ||  temp  >   1000 )
                    
continue ;
                
else
                {
                    e[j].w 
=  temp;
                    e[j].x 
=  i;
                    e[j].y 
=  k;
                    j
++ ;
                }
            }
        }

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

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

        
/* Kruscal过程求最小生成树 */
        ans 
=   0.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);
        }
        
if (ans  >   0 ){
            ans 
*=   100 ;
            printf(
" %.1lf\n " , ans);
        }
        
else  printf( " oh!\n " );
    }
    
return   0 ;
}

 

 

Reproduced in: https: //www.cnblogs.com/ktyanny/archive/2009/12/15/1625049.html

Guess you like

Origin blog.csdn.net/weixin_33895016/article/details/93965841