Building a Space Station

You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task. 
The space station is made up with a number of units, called cells. All cells are sphere-shaped, but their sizes are not necessarily uniform. Each cell is fixed at its predetermined position shortly after the station is successfully put into its orbit. It is quite strange that two cells may be touching each other, or even may be overlapping. In an extreme case, a cell may be totally enclosing another one. I do not know how such arrangements are possible.

All the cells must be connected, since crew members should be able to walk from any cell to any other cell. They can walk from a cell A to another cell B, if, (1) A and B are touching each other or overlapping, (2) A and B are connected by a `corridor', or (3) there is a cell C such that walking from A to C, and also from B to C are both possible. Note that the condition (3) should be interpreted transitively.

You are expected to design a configuration, namely, which pairs of cells are to be connected with corridors. There is some freedom in the corridor configuration. For example, if there are three cells A, B and C, not touching nor overlapping each other, at least three plans are possible in order to connect all three cells. The first is to build corridors A-B and A-C, the second B-C and B-A, the third C-A and C-B. The cost of building a corridor is proportional to its length. Therefore, you should choose a plan with the shortest total length of the corridors.

You can ignore the width of a corridor. A corridor is built between points on two cells' surfaces. It can be made arbitrarily long, but of course the shortest one is chosen. Even if two corridors A-B and C-D intersect in space, they are not considered to form a connection path between (for example) A and C. In other words, you may consider that two corridors never intersect.

Input

The input consists of multiple data sets. Each data set is given in the following format.


x1 y1 z1 r1 
x2 y2 z2 r2 
... 
xn yn zn rn

The first line of a data set contains an integer n, which is the number of cells. n is positive, and does not exceed 100.

The following n lines are descriptions of cells. Four values in a line are x-, y- and z-coordinates of the center, and radius (called r in the rest of the problem) of the sphere, in this order. Each value is given by a decimal fraction, with 3 digits after the decimal point. Values are separated by a space character.

Each of x, y, z and r is positive and is less than 100.0.

The end of the input is indicated by a line containing a zero.

Output

For each data set, the shortest total length of the corridors should be printed, each in a separate line. The printed values should have 3 digits after the decimal point. They may not have an error greater than 0.001.

Note that if no corridors are necessary, that is, if all the cells are connected without corridors, the shortest total length of the corridors is 0.000.

Sample Input

3
10.000 10.000 50.000 10.000
40.000 10.000 50.000 10.000
40.000 40.000 50.000 10.000
2
30.000 30.000 30.000 20.000
40.000 40.000 40.000 20.000
5
5.729 15.143 3.996 25.837
6.013 14.372 4.818 10.671
80.115 63.292 84.477 15.120
64.095 80.924 70.029 14.881
39.472 85.116 71.369 5.553
0

Sample Output

20.000
0.000
73.834 题意:给你一些球的圆心三维坐标及半径,求连接所有球体所需最小长度(接至球面即可)。
思路:Kruskal,给出坐标求生成树。两球圆心距离求出后减去两球半径即为两球距离。
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std; int f[];
double x[],y[],z[],r[];
struct Edge{
int u,v;
double w;
}edge[]; bool cmp(Edge a,Edge b)
{
return a.w<b.w;
} int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
} double kru(int c,int n)
{
int i;
for(i=;i<=n;i++){
f[i]=i;
}
sort(edge+,edge+c+,cmp);
int cnt=;
double ans=;
for(i=;i<=c;i++){
int u=edge[i].u;
int v=edge[i].v;
double w=edge[i].w;
int fu=find(u),fv=find(v);
if(fu!=fv){
ans+=w;
f[fv]=fu;
cnt++;
}
if(cnt==n-) break;
}
if(cnt<n-) return -;
else return ans;
}
int main()
{
int n,c,i,j;
double t;
while(scanf("%d",&n)&&n!=){
c=;
for(i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&x[i],&y[i],&z[i],&r[i]);
}
for(i=;i<=n;i++){
for(j=i+;j<=n;j++){
t=sqrt((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i])+(z[j]-z[i])*(z[j]-z[i]))-r[i]-r[j]; //两球距离
edge[++c].u=i;
edge[c].v=j;
edge[c].w=t<=?:t;
}
}
printf("%.3f\n",kru(c,n));
}
return ;
}

POJ - 2031 Building a Space Station 三维球点生成树Kruskal的更多相关文章

  1. POJ 2031 Building a Space Station【经典最小生成树】

    链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  2. poj 2031 Building a Space Station【最小生成树prime】【模板题】

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepte ...

  3. POJ 2031 Building a Space Station

    3维空间中的最小生成树....好久没碰关于图的东西了.....              Building a Space Station Time Limit: 1000MS   Memory Li ...

  4. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5173   Accepte ...

  5. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...

  6. POJ 2031 Building a Space Station (计算几何+最小生成树)

    题目: Description You are a member of the space station engineering team, and are assigned a task in t ...

  7. POJ 2031 Building a Space Station【最小生成树+简单计算几何】

    You are a member of the space station engineering team, and are assigned a task in the construction ...

  8. POJ 2031 Building a Space Station (prim裸题)

    Description You are a member of the space station engineering team, and are assigned a task in the c ...

  9. poj 2031 Building a Space Station(prime )

    这个题要交c++, 因为prime的返回值错了,改了一会 题目:http://poj.org/problem?id=2031 题意:就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能 ...

随机推荐

  1. Swift 学习笔记 (类和结构体)

    类和结构体是一种多功能且灵活的构造体.通过使用与现存常量 变量 函数完全相同的语法来在类和结构体中定义属性和方法以添加功能. Swift中不需要你为自定义的类和结构体创建独立的结构和实现文件.在Swi ...

  2. NinjaFramework中文教程(简单版)-手把手教程-从零开始

    第一步: 官网http://www.ninjaframework.org/documentation/getting_started/create_your_first_application.htm ...

  3. java基础语言 运算符

    /* ++,--运算符的使用: 单独使用: 放在操作数的前面和后面效果一样.(这种用法是我们比较常见的) 参与运算使用: 放在操作数的前面,先自增或者自减,然后再参与运算. 放在操作数的后面,先参与运 ...

  4. 6410开发板sd卡启动时烧写u-boot.bin以及u-boot-spl-16k.bin步骤

    参考文档:<SMDK6410_IROM_APPLICATION NOTE_REV 1.00>(可以从这里下载到> 参考博客:Tekkaman的博文<u-boot-2010.09 ...

  5. 【LeetCode】种花问题

    假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花 ...

  6. BZOJ 1231 [Usaco2008 Nov]mixup2 混乱的奶牛:状压dp + 滚动数组

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1231 题意: 给你n个数字s[i],问你有多少个排列,使得任意相邻两数字之差的绝对值大于m ...

  7. RQNOJ 622 最小重量机器设计问题:dp

    题目链接:https://www.rqnoj.cn/problem/622 题意: 一个机器由n个部件组成,每一种部件都可以从m个不同的供应商处购得. w[i][j]是从供应商j处购得的部件i的重量, ...

  8. 淘宝双十一页面(Flexible)demo

    下面的代码是看了大漠 使用Flexible实现手淘H5页面的终端适配 做的一个demo. <!DOCTYPE html> <html lang="en" ng-a ...

  9. ACM学习历程—BestCoder 2015百度之星资格赛1006 单调区间(组合数学)

    Problem Description 百小度最近在逛博客,然后发现了一个有趣的问题. 如下图所示,是一个12 位数014326951987 , 它的数字先逐渐变大, 然后变小,再变大,接着变小,又变 ...

  10. TCP头部格式详解,附Wireshark对TCP头部抓包分析

    TCP之所以能为数据通讯提供可靠的传输,主要在于TCP数据包头部功能非常多. 那么,我们先来看看TCP头部格式(RFC 793.1323定义了TCP头部): TCP头部格式中的内容解析如下:(文末还有 ...