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.

n

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 Output20.000

0.000
73.834

解题思路:题意:在一个三位平面上有几个球体,然后输入数据是给你N个球的球心坐标,以及半径。科学家们想要实现各个球之间的接触,也就是有表面的接触。   
当然,两个球之间可能会有相交的地s(a,b) <= 0 ),那么这两个球是不用你新建道路来实现想通的,我们就可以把他们之间的距离设为0,然后再构建一个最小生成树就好,求最短路也一样,都可以解决~ 同时这道题也发现了一个细节吧。
数据位double类型的时候,
用G++的时候scanf要用%lf,而printf的时候要用%f,否则会WA! 代码如下:
 #include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
using namespace std; int n ;
struct edge{
int u ;
int v ;
double w ;
}e[];
struct point{
double x;
double y;
double z;
double r;
}p[];
double get(point a , point b)
{
double ss = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
return ss;
}
bool cmp(edge a ,edge b)
{
return a.w<b.w;
}
double dis ;
int pre[];
int find(int x)
{
return (x==pre[x])?x:pre[x] = find(pre[x]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
dis = ;
int edge_num = ;
for(int i = ; i <= n ;i++)
{
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z,&p[i].r);
}
for(int i = ; i <= n ; i++)
{
pre[i] = i;
}
for(int i = ; i <= n ;i++)
{
for(int j = ; j <= n ;j++)
{
dis = get(p[i],p[j])-(p[i].r+p[j].r); e[edge_num].u = i ;
e[edge_num].v = j;
e[edge_num].w = dis<=0.000001?:dis;
edge_num++; }
}
sort(e,e+edge_num,cmp);
double ans = ;
int u , v ;
double w;
int fx , fy;
for(int i = ; i < edge_num ;i++)
{
u = e[i].u;
v = e[i].v;
w = e[i].w; fx = find(u);
fy = find(v);
if(fx!=fy)
{
pre[fx] = fy;
ans += w;
}
}
printf("%.3lf\n",ans);
}
return ;
}

POJ - 2031C - Building a Space Station最小生成树的更多相关文章

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

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

  2. POJ 2031 Building a Space Station 最小生成树模板

    题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...

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

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

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

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

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

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

  6. POJ 2031:Building a Space Station 最小生成树

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

  7. POJ 2031 Building a Space Station

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

  8. poj 2931 Building a Space Station &lt;克鲁斯卡尔&gt;

    Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5869 Accepted: 2 ...

  9. POJ - 2031 Building a Space Station 三维球点生成树Kruskal

    Building a Space Station You are a member of the space station engineering team, and are assigned a ...

随机推荐

  1. 前端开发之JavaScript HTML DOM理论篇一

    主要内容: 1.DOM简介 2.DOM 节点 3.DOM 方法和属性 4.DOM 访问和修改 一.DOM简介 1.什么是 DOM? DOM 是 W3C(万维网联盟)的标准. DOM 定义了访问 HTM ...

  2. MYSQL的随机查询的实现方法

    的确是那么回事. MYSQL的随机抽取实现方法.举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是:SELECT * FROM tablename ORDER BY RAND() ...

  3. Unity Pitfall 汇总

    [Unity Pitfall 汇总] 1. 当脚本被绑定到一个对象时,一个类对象即会被创建,此意味着此类构造函数会被调用.所以在构造函数中不要调用任何运行时才创建的类.相应的初始化方代码应该移至Sta ...

  4. Python基础:字符串的常见操作

    # 切片 # 切片 获取对象中一部分数据 [起始位置:结束位置(不包含):步长] qpstr = "山东张学友" result = qpstr[1: 3: 1] # 东张 prin ...

  5. 【HDU3853】LOOPS

    题意 有一个R*C的方格.一个人想从(1,1)走到(r,c).在每个格子都有三种选择,向下,向右,或者原地不动.每个格子里的每个选择都有一定的概率.而每次移动都需要消耗2点的能量,问期望消耗的能量是多 ...

  6. 【CF#303D】Rotatable Number

    [题目描述] Bike是一位机智的少年,非常喜欢数学.他受到142857的启发,发明了一种叫做“循环数”的数. 如你所见,142857是一个神奇的数字,因为它的所有循环排列能由它乘以1,2,...,6 ...

  7. php魔术方法__SET __GET

    __SET  设置一个不可访问的属性的时候 调用_set方法 __GET 获取一个不可访问的属性的时候  调用_get 方法 <?php class stu{ private $a; priva ...

  8. Jenkins 更新最新版本

    一般情况下,war的安装路径在/usr/share/jenkins目录下. 不过也有部分人不喜欢安装在这里,可以通过系统管理(System management)--> 系统信息(System ...

  9. Oracle表格字段采用sequence进行自增长时,采用Dbutils进行insert或update数据时的处理技巧

    // 定义插入记录的方法 public Teacher insert(String name, String gender, Double score) { // 获得连接 Connection co ...

  10. 1097G Vladislav and a Great Legend

    传送门 分析 https://blog.csdn.net/forever_shi/article/details/88048528 代码 #include<iostream> #inclu ...