ZOJ 1718 Building a Space Station

        每个cell相当于一个点,但是某些点之间可能相邻,不需要再建走廊,所以只需判断他们在空间中是否相交....

 

        水水kruskal

#include<iostream> #include<stdio.h> #include<stdlib.h> #include<math.h> using namespace std; const int M = 101; const double eps = 1e-8; int p[M]; int find(int x) { return p[x]==x?x:( p[x]=find(p[x]) ); } struct p { double x,y,z; double r; } a[M]; struct edge { int from,to; double w; } b[M*M/2],t; int cmp(const void *a,const void *b) { struct edge aa = *(struct edge *)a; struct edge bb = *(struct edge *)b; if( aa.w > bb.w + eps) return 1; if( aa.w + eps < bb.w) return -1; return 0; } int main(void) { int n,count; int i,j,k,from,to; double sum; while( scanf("%d",&n) != EOF && n) { for( i = 1;i <= n; i++ ) scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z,&a[i].r); for( i = 1; i<= n; i++ ) p[i] = i; for( i = 1,count = 0; i <= n; i++ ) for( j = i+1; j <= n; j++ ) { sum = 0; sum += (a[i].x-a[j].x)*(a[i].x-a[j].x); sum += (a[i].y-a[j].y)*(a[i].y-a[j].y); sum += (a[i].z-a[j].z)*(a[i].z-a[j].z); sum = (sqrt(sum) - a[i].r - a[j].r); if( sum + 1e-8 <= 0 ) { from = find(i); to = find(j); p[from] = to; continue; } t.from = i;t.to = j;t.w = sum; b[++count] = t; } qsort(b+1,count,sizeof(struct edge),cmp); for( i = 1,sum=0; i <= count; i++ ) { from = find( b[i].from ); to = find( b[i].to ); if( from == to ) continue; p[from] = to; sum += b[i].w; } printf("%.3lf/n",sum); } return 0; }

你可能感兴趣的:(struct)