LeeBlog

导航

HDU 1879 继续畅通工程

跟畅通工程差不多,我的代码就在那上面改的
#include<stdio.h>
#include<stdlib.h>
struct e
{
       int x,y,v;
}val[10500];
int n,m,set[105],sum;
int cmp( const void *a,const void *b )
{
    return ( ( e * )a ) -> v - ( ( e * )b ) -> v;
}
int find( int i )
{
    return i == set[i] ? i :set[i] = find( set[i] );
}
void Kustra( )
{
     for( int i = 1; i <= m; ++i )
     {
          int a = val[i].x,b = val[i].y, v = val[i].v;
          a = find( a ),b = find( b );
          if( a != b )
              set[a] = b,sum += v;
      }
 }
int main( )
{
    while( scanf( "%d",&n ) ,n )
    {
           sum = 0;
           m = ( n - 1 ) * n / 2;
           for( int i = 0; i <= n; ++i )
                set[i] = i;
           for( int i = 1; i <= m; ++i )
           {
                int d;
                scanf( "%d%d%d%d",&val[i].x,&val[i].y,&val[i].v,&d );
                if( d )
                    set[find( val[i].x )] = find( val[i].y );
           }
           qsort( val,m,sizeof( val[1] ),cmp );
           Kustra(  );
           int c = 0;
           for( int i = 1;i <= n; ++i )
           {
                if( set[i] == i )
                    ++c;
                }
           printf( "%d\n",sum );
           }
    return 0;
}

posted on 2011-04-30 09:02  LeeBlog  阅读(150)  评论(0编辑  收藏  举报