hdu 1874 畅通工程续
#include<stdio.h> #include<math.h> #include<stdlib.h> struct E { int x,y,set; }e[124]; struct val { int a,b; double dis; }va[10000]; int t,c,count; int cmp( const void *a,const void *b ) { return ((val *)a)->dis > ((val *)b)->dis ? 1 : -1; } int find( int i ) { return e[i].set == i ? i : e[i].set = find( e[i].set ); } double Krustra( ){ double sum = 0; for( int i = 1 ; i <= count; ++i ) { int a = va[i].a,b = va[i].b; a = find( a ),b = find( b ); if( a != b ) { sum += va[i].dis; e[a].set = b; } } return sum; } int main( ) { scanf( "%d",&t ); while( t-- ) { scanf( "%d",&c ); count = 0; for( int i = 0; i <= c; ++i ) e[i].set = i; for( int i = 1; i <= c; ++i ) { scanf( "%d%d",&e[i].x,&e[i].y ); } for( int i = 1; i < c; ++i ) { for( int j = i + 1; j <= c; ++j ) { double a = e[i].x - e[j].x,b = e[i].y - e[j].y; double dis = sqrt( a * a + b * b ); if( dis >= 10 && dis <= 1000 ) { ++count; va[count].a = i,va[count].b = j,va[count].dis = dis; } } } qsort( va + 1,count,sizeof( va[1] ),cmp ); double res = Krustra(); int k = 0; for( int i = 1; i <= c; ++i ) { if( e[i].set == i ) ++k; if( k > 1 ) break; } if( k == 1) printf( "%.1lf\n",res * 100 ); else printf( "oh!\n" ); } return 0; }
本人还是新手 ,转载请注明来自Lvsi‘s home