Building a Space Station

链接

POJ_2031 Building a Space Station

Describe

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 AAA to another cell BBB, if, (1) AAA and BBB are touching each other or overlapping, (2) AAA and B are connected by a `corridor’, or (3) there is a cell CCC such that walking from AAA to CCC, and also from BBB to CCC 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 AAA, BBB and CCC, 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−BA-BA−B and A−CA-CA−C, the second B−CB-CB−C and B−AB-AB−A, the third C−AC-AC−A and C−BC-BC−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−BA-BA−B and C−DC-DC−D intersect in space, they are not considered to form a connection path between (for example) AAA and CCC. 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.

nx1y1z1r1x2y2z2r2...xnynznrnn\\ x_1\ y_1\ z_1\ r_1\\ x_2\ y_2\ z_2\ r_2\\ ...\\ x_n\ y_n\ z_n\ r_nnx1​ y1​ z1​ r1​x2​ y2​ z2​ r2​...xn​ yn​ zn​ rn​

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

The following n lines are descriptions of cells. Four values in a line are xxx-, yyy- and zzz-coordinates of the center, and radius (called rrr 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,zx, y, zx,y,z and rrr is positive and is less than 100.0100.0100.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 333 digits after the decimal point. They may not have an error greater than 0.0010.0010.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.0000.0000.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

解析

几个要点:
1、三维空间内两点坐标公式与二维空间内相比只是多了一个 zzz 坐标的平方;
2、两空间站之间“走廊”的长度是两空间站中心直线距离减去各自的半径 rrr;
3、由要点2易得,判断两空间站是否重合或相交只需要看求出的走廊长度是否为非正整数即可;
4、Kruskal算法要配合并查集算法一起使用;

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
struct str
{int a,b;double val;
}edg[20001];
struct xyz
{double x,y,z,r;
}a[201];
int cnt;
double ans;
int n,pre[201],fx,fy;
void add(int frm,int to,double val)
{cnt++;edg[cnt].a=frm;edg[cnt].b=to;edg[cnt].val=val;
}
bool cmp(str a,str b)
{return a.val<b.val;
}
double dis(xyz a,xyz b)
{double xx,yy,zz,tmp;xx=(a.x-b.x)*(a.x-b.x);yy=(a.y-b.y)*(a.y-b.y);zz=(a.z-b.z)*(a.z-b.z);tmp=sqrt(xx+yy+zz);if(tmp-(a.r+b.r)>0)return tmp-(a.r+b.r);elsereturn 0;
}
int find_father(int x)
{if(pre[x]==x)return x;elsereturn pre[x]=find_father(pre[x]);
}
int main()
{while(1){scanf("%d",&n);if(!n)break;ans=0,cnt=0;for(int i=1;i<=n;i++)pre[i]=i;for(int i=1;i<=n;i++)cin>>a[i].x>>a[i].y>>a[i].z>>a[i].r;for(int i=1;i<=n-1;i++)for(int j=i+1;j<=n;j++)if(!dis(a[i],a[j])){fx=find_father(i);fy=find_father(j);if(fx!=fy)pre[fy]=fx;}elseadd(i,j,dis(a[i],a[j]));sort(edg+1,edg+cnt+1,cmp);for(int i=1;i<=cnt;i++){fx=find_father(edg[i].a);fy=find_father(edg[i].b);if(fx!=fy){pre[fy]=fx;ans+=edg[i].val;}}printf("%.3f\n",ans);}return 0;
}

POJ_2031 Building a Space Station相关推荐

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

    题目链接:http://poj.org/problem?id=2031点击打开链接 Building a Space Station Time Limit: 1000MS   Memory Limit ...

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

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

  3. POJ 2031 Building a Space Station

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

  4. zoj 1718 poj 2031 Building a Space Station

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

  5. poj2031 Building a Space Station

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

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

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

  7. Building a Space Station--POJ 2031

    1.题目类型:计算几何,最小生成树. 2.解题思路:(1)获得所有点路径长度的矩阵map[][]:(2)利用Prim算法求解最小生成树. 3.注意事项:数学操作,中间值全部用double保存. 4.实 ...

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

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

  9. 2019 ICPC Asia Nanjing Regional I. Space Station题解

    文章目录 [题目链接] [前言] [题目大意] [解题思路] 一.优先暴力 二.记忆化 三.unordered_map以及思维优化 四.乘法逆元(拓展内容) [后记] [题目链接] https://n ...

最新文章

  1. (chap4 IP协议) 路由控制表(Routing Table)
  2. [shell基础]——sed命令
  3. Bootloader加载过程分析
  4. 12 Django cooking与session
  5. UOJ 405(IOI2018 D1T1)
  6. 修改autor后面邮箱_如何修改LOL手游昵称
  7. LeetCode 1473. 给房子涂色 III(DP)
  8. observer 观察者模式
  9. 史上最全的微信小程序代码大全源码下载
  10. MPMoviePlayerController 电影播放器—IOS开发
  11. 使用dea-toolbox进行数据包络分析
  12. java 字符表 chr3,ASCII码对应表chr(9)、chr(10)、chr(13)、chr(32)、chr(3...
  13. 影响债市行情的主要因素_决定债券收益的十大因素
  14. windows四种编码方式
  15. 自动爬取微博热门评论和点赞数并存为EXCEL文件(python2)
  16. excel从身份证号码中获取邮编信息?
  17. 生产系统执行下线操作--泪奔
  18. Java遍历包中所有类包括jar包(完整转载)
  19. 科罗拉多州立大学计算机科学,科罗拉多州立大学的世界排名
  20. 线程同步,互斥 事件 和关键代码的比较

热门文章

  1. Java项目:会员卡积分管理系统(java+JSP+JavaScript+HTML+Mysql)
  2. 用树莓派做相机,制作定格动画视频
  3. 基于Java的电梯模拟系统
  4. (OpenCV — 7)ROI 区域图像叠加&图像混合
  5. 系统引导恢复EasyBCD
  6. matlab 基金业绩归因,基金专题报告:基于净值和持仓的基金业绩归因方法研究...
  7. RuntimeError: Unable to find a valid cuDNN algorithm to run convolution
  8. 如何设置修改WPS批注上的用户信息名称
  9. 爬虫必备技术之解析Html工具库
  10. 微信信息会经过根服务器么,小心,微信这个功能会把包括你性取向在内的信息暴漏的一览无余!...