LeeBlog

导航

hdu 1875 畅通工程再续

这是我第二次敲这个题,没想到果断悲剧了,最后只能翻出以前的代码,没想到是qsort出问题了,以后qsort全部用问号表达式,不再用a-b了.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int set[105],t,c,m;
double sum = 0;
struct e
{
       int x,y;
       double v;
}val[10000];
struct coor
{
       int x,y;
}co[105];
int cmp( const void *a,const void *b )
{
    return ((e *)a) -> v > ((e *)b) -> v ? 1 : -1;//错在这里
}
int find( int x )
{
    return x == set[x] ? x : set[x] = find( set[x] );
}
void Kustra(  )
{
     for( int i = 0; i < m; ++i )
     {
          int x = val[i].x,y = val[i].y;
          if( find(x) != find( y ) )
          {
              set[find(x)] = find(y);
              sum += val[i].v;
          }
      }
 }
int main( )
{
    scanf( "%d",&t );
    while( t-- )
    {
           scanf( "%d",&c );
           m = 0;
           sum = 0;
           for( int i = 0; i < 105; ++i )
                set[i] = i;
           for( int i = 1; i <= c; ++i )
                scanf( "%d%d",&co[i].x,&co[i].y );
           for( int i = 1; i <= c; ++i )//找任意两点之间的距离
           {
                for( int j = 1; j < i; ++j )
                {
                     int x1 = co[i].x,x2 = co[j].x,y1 = co[i].y,y2= co[j].y;
                     double d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
                     if( d >= 10 && d <= 1000 )
                     {
                         val[m].x = i;
                         val[m].y = j;
                         val[m].v = d;
                         ++m;
                     }
                 }
            }
             qsort( val,m,sizeof( val[1] ),cmp );
             Kustra( );
             int f = 0;
             for( int i = 1; i <= c; ++i )
                  if( set[i] == i )
                      ++f;
             if( f != 1 )
                 puts( "oh!" );
             else
                 printf( "%.1lf\n",sum*100 );
    }
    return 0;
}

posted on 2011-02-27 19:45  LeeBlog  阅读(362)  评论(0编辑  收藏  举报