hdu1875-畅通工程再续

http://acm.hdu.edu.cn/showproblem.php?pid=1875

这是我做过最恶心的最小生成树的题目 ,本来看有一段时间没做这个l类型的题目了,拿个题目练练手,结果。。。一整个晚上就卡这个该死的题目上面,我就想不通为什么sort会wa,但是qsort却AC了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
using namespace std ;
const int maxn = 105 ;
struct node 
{
	int x , y ;
	double value ;
}edge[ maxn * ( maxn - 1 ) / 2 ] ;
int n ;
int fa[ maxn ] ;
int temp[ maxn ][ 2 ] ;
/*
int cmp( node a , node b )
{
	return a.value > b.value ;
}
*/
int cmp( const void *a ,const void * b )
{
	return ( (*(node*)a).value - (*(node*)b).value ) > 0 ? 1 : -1 ;
}
int find( int x )
{
	return fa[ x ] = x == fa[ x ] ? x : find( fa[ x ] ) ;
}
int main()
{
	int Case ;
	int i , j , k ;
	scanf( "%d" , &Case ) ;
	while( Case-- )
	{
		scanf( "%d" , &n ) ; 
		for( i = 1 ; i <= n ; i++ )
		{
				scanf( "%d %d" , &temp[ i ][ 0 ] , &temp[ i ][ 1 ] ) ;
				fa[ i ] = i ;
		}
		double temp1 ;
		k = 0 ;
		for( i = 1 ; i <= n ; i++ )
		{
			for( j = i + 1 ; j <= n ; j++ )
			{
				temp1 = sqrt( ( temp[ i ][ 0 ] - temp[ j ][ 0 ] ) * ( temp[ i ][ 0 ] - temp[ j ][ 0 ] ) + ( temp[ i ][ 1 ] - temp[ j ][ 1 ] ) * ( temp[ i ][ 1 ] - temp[ j ][ 1 ] ) ) ;
				if( temp1 < 10 || temp1 > 1000 )
					continue ;
				else
				{
					edge[ k ].x = i ;
					edge[ k ].y = j ;
					edge[ k ].value = temp1 ;
					k++ ;
				}
			}
		}
		//sort( edge , edge + k , cmp ) ;
		qsort( edge , k , sizeof( edge[ 0 ] ) , cmp ) ;
		int sum = 0; 
		double ans = 0.0 ;
		for( i = 0 ; i < k ; i++ )
		{
			int x1 = find( edge[ i ].x );
			int x2 = find( edge[ i ].y );
			if( x1 != x2 )
			{
				fa[ x1 ] = x2 ;
				sum++ ;
				ans += edge[ i ].value ;
			}
			if( sum == n - 1 )
				break ;
		}
		if( sum == n - 1)
		{
			printf( "%.1lf\n" ,ans * 100 ) ;
		}
		else
			printf( "oh!\n" ) ;
	}
	return 0 ;	
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值