POJ-2031-Building a Space Station

来源:互联网 发布:外星人调节灯光软件 编辑:程序博客网 时间:2024/06/03 18:12
Building a Space Station
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8178 Accepted: 3927

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.

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

310.000 10.000 50.000 10.00040.000 10.000 50.000 10.00040.000 40.000 50.000 10.000230.000 30.000 30.000 20.00040.000 40.000 40.000 20.00055.729 15.143 3.996 25.8376.013 14.372 4.818 10.67180.115 63.292 84.477 15.12064.095 80.924 70.029 14.88139.472 85.116 71.369 5.5530

Sample Output

20.0000.00073.834

Source

Japan 2003 Domestic


题意:

该题说的是在太空中有n个基本单位,现在有它们的空间坐标x,y,z和半径r;现在让你求要让各个基本单位连结起来需要添加的长廊最短要多少(两个基本单位只要有接触,或则重叠部分就视为连结)。


可以看出来这是一个类似于三维的最小生成树,就是在求两点之间的距离时,如果两基本单位连结,那么它们的距离就为0。

求两基本的距离的公式为:double Dis = sqrt((a[u].x-a[v].x)*(a[u].x-a[v].x) + (a[u].y-a[v].y)*(a[u].y-a[v].y) + (a[u].z-a[v].z)*(a[u].z-a[v].z))-a[u].r-a[v].r;

其余操作和简单的最小生成树的操作是一样的。


#include <cstdio>#include <iostream>#include <cstring>#include <cstdlib>#include <algorithm>#include <cmath>const int Maxn = 110;#define INF 0x3f3f3f3fusing namespace std;struct node{double x,y,z,r;}a[Maxn];int n;double Map[Maxn][Maxn],vist[Maxn],dis[Maxn];double distance(int u,int v){double Dis = sqrt((a[u].x-a[v].x)*(a[u].x-a[v].x) + (a[u].y-a[v].y)*(a[u].y-a[v].y) + (a[u].z-a[v].z)*(a[u].z-a[v].z))-a[u].r-a[v].r;if(Dis < 0)return 0;return Dis;}void prim(){memset(vist,0,sizeof(vist));for(int i = 0; i < n; i++)dis[i] = Map[0][i];vist[0] = 1;int pos;double Min,sum = 0;for(int i = 1; i < n; i++){Min = INF;for(int j = 0; j < n; j++)if(!vist[j] && Min > dis[j]){Min = dis[j];pos = j;}sum += Min;vist[pos] = 1;for(int j = 0; j < n; j++)if(!vist[j] && dis[j] > Map[pos][j])dis[j] = Map[pos][j];}printf("%.3lf\n",sum);}int main(){while(~scanf("%d",&n) && n){memset(Map,0,sizeof(Map));for(int i = 0; i < n; i++)scanf("%lf %lf %lf %lf",&a[i].x,&a[i].y,&a[i].z,&a[i].r);for(int i = 0; i < n; i++)for(int j = 0; j < n; j++)if(i == j)Map[i][j] = 0;elseMap[i][j] = distance(i,j);prim();}return 0;}


1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子该上初三了英语基础差怎么办 初二学生辍学半年又想上学怎么办 保险用的小红本丢了怎么办 孩子的小红本丢了怎么办 宝宝打针的小红本丢了怎么办 居民养老保险小红本丢了怎么办 小学六年级素质报告单丢了怎么办 西安小学素质报告册有b怎么办 辽宁省干部在线学习时长不够怎么办 鬼火一代发动机在中间卡死怎么办 医疗权与患者隐私权想冲突怎么办 登录法宣在线出现贴号失效怎么办 黔微普法在线竞答成绩不合格怎么办 怎么办刮过胡子后又长了? 网贷暴力催收警察也没办法怎么办 儿子把我车抵押给了高利贷怎么办 水泵开机五分钟左右就跳闸怎么办 离婚了她说过的不好你该怎么办 偏侧咀嚼笑时一边嘴歪怎么办 三相四线开关前地线有电怎么办 高三学生重度抑郁怎么办马高考了 潞城报名系统密码找不回来怎么办 绵阳富乐实验小学外地入学怎么办 联想手机升级失败开不了机怎么办 电脑桌面上的微信图标不见了怎么办 精子总活力和运动力低怎么办 精子少畸形高怎么办能治好吗 小米无人机只能飞500米远怎么办 考察课作弊被老师抓了怎么办 手机中木马病毒了钱被盗走了怎么办 在搜不到校园网的情况下怎么办 刚买小鸡第四天晚上一直叫怎么办 碘131第四天晚上吃太撑了怎么办 出现请点击画面开始进行校准怎么办 大学毕业后才发现荒废了学业怎么办 房屋面积重新测量超了一倍怎么办 物管把业主的电断了怎么办 在人多的场合紧张不适应怎么办 人寿人身损害勘查结果有争议怎么办 批量性不良再限度样本规格内怎么办 老公说老婆不攒钱都买衣服了怎么办