题目链接:http://poj.org/problem?id=2031点击打开链接

Building a Space Station
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9824   Accepted: 4621

Description

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

浮点型的最小生成树

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<math.h>
using namespace std;
map<int ,int > mmap;
struct xjy
{int frm;int to;double dis;bool operator < (const xjy &r)const{return dis<r.dis;}
};
struct point
{double x;double y;double z;double r;
};
vector <xjy> vvector;
vector <xjy > ::iterator it;
vector <point> vectorr;
int pre[111];
int findx(int x)
{int rr=x;while(rr!=pre[rr])rr=pre[rr];return rr;
}
int main()
{//ios::sync_with_stdio (false);int n;while(~scanf("%d",&n)&&n){for(int i=0;i<=111;i++)pre[i]=i;vectorr.clear();vvector.clear();for(int i=0;i<n;i++){point mid;cin >> mid.x >> mid.y >> mid.z >> mid.r;vectorr.push_back(mid);}for(int i=0;i<vectorr.size();i++)for(int j=i+1;j<vectorr.size();j++){xjy mid;mid.dis=(sqrt(pow((vectorr[i].x-vectorr[j].x),2) + pow((vectorr[i].y-vectorr[j].y),2) + pow((vectorr[i].z-vectorr[j].z),2))-vectorr[i].r-vectorr[j].r)*1.0;if(mid.dis<0)mid.dis=0;mid.frm=i;mid.to=j;vvector.push_back(mid);}sort(vvector.begin(),vvector.end());double sum=0;for(int i=0;i<vvector.size();i++){if(findx(vvector[i].frm)!=findx(vvector[i].to)){sum+=vvector[i].dis;pre[findx(vvector[i].frm)]=findx(vvector[i].to);}}printf("%.3f\n",sum);}
}

POJ - 2031 Building a Space Station (最小生成树)相关推荐

  1. pku 2031 Building a Space Station 最小生成树+精度控制

    http://poj.org/problem?id=2031 题意就是给你n个球的(球心以及半径),然后求最小生成树就欧了,这里的关键部分是对于重复的球的路径长度的处理 #include <io ...

  2. POJ 2031 Building a Space Station

    题目链接:http://poj.org/problem?id=2031 题目意思是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则算为已连通,无需再 ...

  3. zoj 1718 poj 2031 Building a Space Station

    最小生成树,用了Kruskal算法.POJ上C++能过,G++不能过... 算出每两个圆心之间的距离,如果距离小于两半径之和,那么这两个圆心之间的距离直接等于0,否则等于距离-R[i]-R[j]. # ...

  4. POJ_2031 Building a Space Station

    Building a Space Station 链接 POJ_2031 Building a Space Station Describe You are a member of the space ...

  5. poj2031 Building a Space Station

    题目链接:http://poj.org/problem?id=2031 题意:给你n个球,让你用最小的花费,把这n个球联通,如果两个球相交,就默认联通,否者连接这两个球的花费就是,球面之间的最短距离, ...

  6. I. Space Station(hash记忆化+dp)

    <文章>陆游 文章本天成,妙手偶得之. 粹然无疵瑕,岂复须人为. 君看古彝器,巧拙两无施. 汉最近先秦,固已殊淳漓. 胡部何为者,豪竹杂哀丝. 后夔不复作,千载谁与期? I. Space ...

  7. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得随意牛棚两个 ...

  8. 【POJ 1788 --- Building a New Depot】

    [POJ 1788 --- Building a New Depot] Description Advanced Cargo Movement, Ltd. is successfully expand ...

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

    1 /* 2 最小生成树 + 几何判断 3 Kruskal 球心之间的距离 - 两个球的半径 < 0 则说明是覆盖的!此时的距离按照0计算 4 */ 5 #include<iostream ...

最新文章

  1. ASP.NET MVC 5 - 视图
  2. Python内置数据结构之双向队列
  3. 【随笔】通用近似定理
  4. HarmonyOS之跨设备分布式应用调试
  5. cmake 编译安装方法
  6. 高校教师实名举报基金申报书被同事抄袭,抄袭者顺利晋升职称!
  7. Web前后端笔记-通过对称加密算法和信息摘要算法防止数据重放
  8. 【WPF】如何保存RichTextBox的文本到数据库?以及如何对RichTextBox的Document做绑定?...
  9. python signal
  10. JSTL中的Core标签库
  11. 微信小程序CSS之Flex布局
  12. 如何利用labelImg软件标图
  13. 【从零开始学架构-李运华】04|复杂度来源:高性能
  14. 完美解决Tensorflow不支持AVX2指令集问题|指令集加速
  15. mysql合并两个表_MYSQL如何合并两个表
  16. HTML+CSS+JS网站设计——上海美食(8页) 酒水网页设计作业,甜品美食网页制作作业, 学生零食网页作业
  17. Loadrunner_集合点(rendezvous)
  18. java计算机毕业设计校园讲座管理源码+系统+lw文档+mysql数据库+部署
  19. android更改键盘布局,如何通过按Shift键更改android的键盘布局
  20. 几十年前的老旧照片如何修复?还不知道旧照片怎么恢复清晰吗?

热门文章

  1. 《亿级流量网站架构核心技术》读后感
  2. 开环零点与闭环零点对系统的影响
  3. 论文翻译——vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases
  4. 惠普战66proG1 BIOS/固件设置Intel VT-x为enable
  5. Incorrect string value: '\xF0\x9F\x92\x98\xF0\x9F...'
  6. 印度4G运营商主导市场对中国手机企业并非好消息
  7. 【世界是自己的,与他人毫无关系】--杨绛
  8. 洛谷——【入门2】分支结构
  9. 从0搭建Android直播系统
  10. linux git 401,git push 提示 401 Unauthorized while accessing https 的原因及解决方法