标签:
Description
Input
Output
Sample Input
Sample Output
#include<stdio.h> #include<algorithm> #include<math.h> #define Max 0x3f3f3f3f using namespace std; struct island { double x , y ; }land[110]; int n , fa[110] ; int in = 1 ; struct edge { int u , v ; double w ; }e[105*52+2]; void binary (island a , int key , int l , int r ) { if ( l >= r) { return ; } int mid = (l + r) / 2 ; binary (a , key , l , mid ) ; binary (a , key , mid + 1 , r ) ; double dis = (a.x - land[mid].x) * (a.x - land[mid].x) + (a.y - land[mid].y) * (a.y - land[mid].y) ; // printf ("dis=%.1f\n" , dis ) ; if ( dis <= 1000 * 1000 && dis >= 10 * 10 ) { e[in].u = key ; e[in].v = mid ; e[in].w = sqrt (dis) ; in++ ; } } bool cmp (edge a , edge b) { return a.w < b.w ; } int find (int x) { return fa[x] == x ? x : find (fa[x]) ; } void kruskal () { sort ( e + 1 , e + in , cmp ) ; int x , y ; double ans = 0; for (int i = 1 ; i < in ; i++) { x = find (e[i].u) ; y = find (e[i].v) ; if ( x != y) { ans += e[i].w ; fa[x] = y ; } } printf ("%.1f\n" , 100.0 * ans ) ; } int main () { // freopen ("a.txt" , "r" , stdin ) ; int T; scanf ("%d" , &T ) ; int x , y ; int cnt ; while (T--) { cnt = 0 ; scanf ("%d" , &n ) ; for (int i = 1 ; i <= n ; i++) fa[i] = i ; for (int i = 1 ; i <= n ; i++) { scanf ("%lf%lf" , &land[i].x , &land[i].y ) ; } for (int i = 1 ; i < n ; i++ ) {//建立 u , v , w binary ( land[i] , i , i + 1 , n + 1) ; } /*for (int i = 1 ; i < in ; i++) { printf ("u=%d v=%d w=%.1f\n" , e[i].u , e[i].v , e[i].w ) ; } puts ("") ;*/ //To determine whether the connectivity for (int i = 1 ; i <= n ; i++) { x = find (e[i].u) ; y = find (e[i].v) ; fa[x] = y; } for (int i = 1 ; i <= n ; i++) { if (fa[i] == i ) cnt++ ; if (cnt > 1) break ; } for (int i = 1 ; i <= n ; i++) fa[i] = i ; if ( cnt > 1) puts ("oh!") ; else kruskal () ; } return 0 ; }
标签:
原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4282411.html