poj 2031Building a Space Station(几何判断+Kruskal最小生成树)

简介:
/*
  最小生成树 + 几何判断
  Kruskal      球心之间的距离 - 两个球的半径 < 0 则说明是覆盖的!此时的距离按照0计算 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int f[105];
struct ball{
   double x, y, z, r;
};

struct connect{
   double dist;
   int a, b;
};

connect c[5005];

ball b[105];

bool cmp(connect a, connect b){
   return a.dist < b.dist;
}
int n;

int getFather(int x){
   return x==f[x] ? x : f[x]=getFather(f[x]); 

int Union(int a, int b){
    int fa=getFather(a), fb=getFather(b);
    if(fa!=fb){
        f[fa]=fb;
        return 1;
    }
    return 0;
}

int main(){
   int i, j;
   while(scanf("%d", &n) && n){
      for(i=1; i<=n; ++i)
         scanf("%lf%lf%lf%lf", &b[i].x, &b[i].y, &b[i].z, &b[i].r);
      int cnt=0;
      for(i=1; i<n; ++i)
         for(j=i+1; j<=n; ++j){
             double d = sqrt((b[i].x-b[j].x)*(b[i].x-b[j].x) + (b[i].y-b[j].y)*(b[i].y-b[j].y) + (b[i].z-b[j].z)*(b[i].z-b[j].z))
                        - (b[i].r + b[j].r);
               c[cnt].dist= d<0 ? 0: d;
               c[cnt].a=i; 
               c[cnt++].b=j;
         }
       sort(c, c+cnt, cmp); 
       double minSum=0.0;
       for(i=1; i<=n; ++i)
          f[i]=i;
       for(i=0; i<cnt; ++i){
          if(Union(c[i].a, c[i].b))
             minSum+=c[i].dist;
       }
       printf("%.3lf\n", minSum);
   }
   return 0;
}









本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/3877502.html,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
Go
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
28 0
AtCoder Beginner Contest 216 D - Pair of Balls (思维建图 拓扑排序判断有向图是否有环)
AtCoder Beginner Contest 216 D - Pair of Balls (思维建图 拓扑排序判断有向图是否有环)
88 0
CF1272 E.Nearest Opposite Parity(反向建图+BFS)
CF1272 E.Nearest Opposite Parity(反向建图+BFS)
75 0
CF1272 E.Nearest Opposite Parity(反向建图+BFS)
UPC Graph (最小生成树 || 并查集+二分)
UPC Graph (最小生成树 || 并查集+二分)
69 0
CF1076D.Edge Deletion(最短路径 贪心)
CF1076D.Edge Deletion(最短路径 贪心)
49 0
CF191C——Fools and Roads(树上边差分+LCA)
CF191C——Fools and Roads(树上边差分+LCA)
103 0

热门文章

最新文章