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. springmvc jpa

    昨天帮同学搭建了一个springmvc+jpa+beetl模板引擎的项目环境,供参考. https://files.cnblogs.com/files/startnow/lntu-demo.zip 数 ...

  2. 创建数据库sql语句

    create database JXGL; go create table S( sno char(10)primary key not null, sname nvarchar(10) not nu ...

  3. 【ZOJ 3228】Searching the String 【AC自动机】

    题意 给出n个模式串和一个文本串,输出各个模式串在文本串中出现的次数.模式串有两种类型,0类型代表可以有重叠,1类型代表不能有重叠.模式串可能出现重复. 分析 算是AC自动机的模板题? 因为模式串可以 ...

  4. 关于jdk7中 使用Collections的排序方法时报Comparison method violates its general contract!异常

    参考: Comparison method violates its general contract Comparison method violates its general contract! ...

  5. 2015年2月编程语言排行榜:JavaScript排名达到历史最高

    JavaScript在赢得2014年最后一个月的TIOBE编程语言奖后,并且还在不断走强.在二月份JavaScript就超过了PHP,并 且达到它有史以来最高的位置,排行到TOP 6.另一方面,Obj ...

  6. SQLSERVER Tempdb的作用及优化

    tempdb 系统数据库是可供连接到 SQL Server 实例的所有用户使用的全局资源.tempdb 数据库用于存储下列对象:用户对象.内部对象和版本存储区. 用户对象 用户对象由用户显式创建.这些 ...

  7. zigbee组播通信原理

    组播: 在zigbee网络里面,把网络节点标记为组的方式来进行通信:发送模块如果发送的组号和网络里标记模块的组号相对应,那么这些模块就可以拿到这些无线数据包. 特点: 1.分组中组的编号有两个字节. ...

  8. Spring AOP 整理

    在 xml中加 xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.o ...

  9. 白盒测试实践--Day3 12/19/2017

    累计完成任务情况: 阶段内容 参与人 完成静态代码检查结果报告 小靳 完成JUnit脚本编写 小黄 完成CheckStyle检查 小靳 完成代码评审会议纪要和结果报告 小熊.小梁及其他 完成白盒测试用 ...

  10. 【转载】MYSQL模式匹配:REGEXP和like用法

    转载地址:http://www.webjx.com/database/mysql-32809.html like like要求整个数据都要匹配,而REGEXP只需要部分匹配即可. 也就是说,用Like ...