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

时间:2022-01-01 09:22:42
Building a Space Station
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5173   Accepted: 2614

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

判断两球表面距离是否小于零,即球心距 - 半径1 - 半径2 <= 0,如果满足的话就合并掉,不改变答案值,然后跑一边kruskal就行。

 #include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
int FATHER[SIZE],N,NUM;
struct Node
{
int from,to;
double cost;
}G[SIZE * SIZE];
struct
{
double x,y,z,r;
}TEMP[SIZE]; void ini(void);
int find_father(int);
void unite(int,int);
bool same(int,int);
bool comp(const Node &,const Node &);
double kruskal(void);
double dis(double,double,double,double,double,double);
int main(void)
{
while(~scanf("%d",&N))
{
if(!N)
break;
ini();
for(int i = ;i <= N;i ++)
scanf("%lf%lf%lf%lf",&TEMP[i].x,&TEMP[i].y,&TEMP[i].z,&TEMP[i].r);
for(int i = ;i <= N;i ++)
for(int j = i + ;j <= N;j ++)
{
G[NUM].from = i;
G[NUM].to = j;
G[NUM].cost = dis(TEMP[i].x,TEMP[i].y,TEMP[i].z,TEMP[j].x,TEMP[j].y,TEMP[j].z)
- TEMP[i].r - TEMP[j].r;
if(G[NUM].cost <= )
unite(i,j);
NUM ++;
}
sort(G,G + NUM,comp);
printf("%.3f\n",kruskal());
} return ;
} void ini(void)
{
NUM = ;
for(int i = ;i <= N;i ++)
FATHER[i] = i;
} int find_father(int n)
{
if(n == FATHER[n])
return n;
return FATHER[n] = find_father(FATHER[n]);
} void unite(int x,int y)
{
x = find_father(x);
y = find_father(y); if(x == y)
return ;
FATHER[x] = y;
} bool same(int x,int y)
{
return find_father(x) == find_father(y);
} bool comp(const Node & a,const Node & b)
{
return a.cost < b.cost;
} double kruskal(void)
{
double ans = ; for(int i = ;i < NUM;i ++)
if(!same(G[i].from,G[i].to))
{
unite(G[i].from,G[i].to);
ans += G[i].cost;
}
return ans;
} double dis(double x_1,double y_1,double z_1,double x_2,double y_2,double z_2)
{
return sqrt(pow(x_1 - x_2,) + pow(y_1 - y_2,) + pow(z_1 - z_2,));
}

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

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

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

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

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

  3. POJ 2031&Tab; Building a Space Station (最小生成树)

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

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

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

  5. POJ 2031 Building a Space Station

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

  6. 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 ...

  7. POJ 2031 Building a Space Station &lpar;计算几何&plus;最小生成树&rpar;

    题目: Description You are a member of the space station engineering team, and are assigned a task in t ...

  8. POJ 2031 Building a Space Station【最小生成树&plus;简单计算几何】

    You are a member of the space station engineering team, and are assigned a task in the construction ...

  9. POJ - 2031C - Building a Space Station最小生成树

    You are a member of the space station engineering team, and are assigned a task in the construction ...

随机推荐

  1. 【Oracle】oracle中快速判断某一日期是闰年或平年

    )),' then '平年' else '闰年' end as isLeapYear from dual 第一步:取日期的年初日期:第二步:年初日期增加一个月即概念2月:第三步:取概念2月的最后一天的 ...

  2. Azure China &lpar;6&rpar; SAP 应用在华登陆 Windows Azure 公有云

    <Windows Azure Platform 系列文章目录>     2014年07月11日 由世纪互联运营的 Windows Azure 为 SAP 应用提供公有云平台 2014 年 ...

  3. linux包之findutils之find和xargs命令

    解释 [root@localhost ~]# rpm -qa|grep findfindutils-4.4.2-6.el6.x86_64 find 路径 测试 动作 三个步骤的处理过程查找一个或多个目 ...

  4. js动态加载脚本

    最近公司的前端地图产品需要做一下模块划分,希望用户用到哪一块的功能再加载哪一块的模块,这样可以提高用户体验. 所以到处查资料研究js动态脚本的加载,不过真让人伤心啊!,网上几乎都是同一篇文章,4种方法 ...

  5. C&num;几种截取字符串的方法小结,需要的朋友可以参考一下

    1.根据单个分隔字符用split截取 例如 复制代码 代码如下: string st="GT123_1"; string[] sArray=st.split("_&quo ...

  6. Dynamics CRM2011 通过DeveloperToolkit在VS中部署遇到的问题

    接上一篇继续,说个在部署的过程中我遇到了个问题:"Error registering plugins and/or workflows. Plug-in assembly does not ...

  7. 关于 i&plus;&plus; 与 &plus;&plus;i

    首先关于这个真的不想在看资料了,自己都背过了,++i 和 i++,在单独使用时,就是 i=i+1. a = ++i        相当于 i=i+1; a = i; (先i = i + 1,再使用i的 ...

  8. ToroiseSVN和VisualSVN-server的配置使用&comma; 外网访问SVN 版本库

    https://www.cnblogs.com/Leo_wl/p/3475167.html

  9. win7 蓝屏信息获取和处理

    一.先说电脑蓝屏原因和解决方法: 1.驱动不对,驱动和硬件不兼容出现的问题,这个直接卸载软件或者重装驱动. 2.内存条有问题或者内存损坏:这个内存条很可能是没插紧,内存损坏的话,换个内存条. 3.病毒 ...

  10. angularjs探秘&lt&semi;五&gt&semi; 举足轻重的scope

    scope在angular中的作用可谓举足轻重,不理解scope就不会angular: scope是应用在 HTML (view) 和 JavaScript (controller)之间的纽带. sc ...