Building a Space Station
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6083   Accepted: 3024

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

题意是给了一堆三维坐标下的空间站,空间站都是球形,有其球心坐标和半径。问把这些空间站都连接起来,问最小代价。

就是求最小生成树,两个空间站T1 T2的距离是坐标下的距离 - T1的半径 - T2的半径

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct no{
double x;
double y;
double z;
double r;
}node[105]; int num,n;
double map[102][102];
int stack[102];
double minidis[102]; double prim()
{
int i,j,s;
double result; memset(stack,0,sizeof(stack));
for(i=1;i<=num;i++)
{
minidis[i]=100000005;
} stack[1]=1;
minidis[1]=0;
s=1;
result=0; for(i=1;i<=num-1;i++)
{
double min_all=100000005;
int min_temp=0;
for(j=2;j<=num;j++)
{
if(stack[j]==0&&minidis[j]>map[s][j])
{
minidis[j]=map[s][j];
}
if(stack[j]==0&&minidis[j]<min_all)
{
min_temp=j;
min_all=minidis[j];
}
}
s=min_temp;
stack[s]=1;
result += min_all;
}
return result;
} int main()
{
int i,j;
while(cin>>n)
{
if(n==0)
break;
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
cin>>node[i].x>>node[i].y>>node[i].z>>node[i].r;
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
map[j][i]=map[i][j]=max(0.0,sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x) +(node[i].y-node[j].y)*(node[i].y-node[j].y) +(node[i].z-node[j].z)*(node[i].z-node[j].z))-node[i].r-node[j].r);
}
}
num=n;
printf("%.3lf\n",prim());
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2031: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

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

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

  8. POJ 2031 Building a Space Station (计算几何+最小生成树)

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

  9. POJ 2031 Building a Space Station【最小生成树+简单计算几何】

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

  10. 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. redis中关闭rdb跟aof

    https://zm10.sm-tc.cn/?src=l4uLj8XQ0IiIiNGdip2KlJDRnJCS0JaRmZCbmouelpPSzc%2FJz8vJxtGXi5KT&uid=49 ...

  2. 「JLOI2014」聪明的燕姿

    传送门 Luogu 解题思路 很容易想到直接构造合法的数,但是这显然是会T飞的. 我们需要考虑这样一件事: 对于一个数 \(n\),对其进行质因数分解: \[n=\sum_{i=1}^x p_i^{c ...

  3. spingboot中使用scheduled设置定时任务注意事项

    在spring开发过程中经常会遇到需要定时执行的任务,如定时生成报表,定时推送消息等任务. springboot 提供了简单的 @Scheduled 表达式来配置定时任务.该方式默认是单线程的,任务在 ...

  4. 隐患写法flag.equals("true")带来的空指针异常

    分类:2008-06-04 12:47 467人阅读 评论(0) 收藏 举报 linuxjava测试 昨天,有同事A对同事B写的程序进行测试时,出现错误,看控制台信息,发现抛出了空指针异常. 调查结果 ...

  5. CSS-font

    font:[ [ <' font-style '> || <' font-variant '> || <' font-weight '> ]? <' font ...

  6. 三 Road

    3—5年程序员的发展和出路在哪里? 是继续做技术人,还是向管理者发力?是继续留在大公司,还是转投潜力小公司?如果没有核心竞争力,入行一两年的新程序员朋友是可以替代你大部分工作的,而且薪资还低,要怎么办 ...

  7. CSP-J/S2019试题选做

    S D1T2 括号树 设\(f[u]\)表示根到\(u\)的路径上有多少子串是合法括号串.(即题目里的\(k_u\),此变量名缺乏个性,故换之) 从根向每个节点dfs,容易求出\(c[u]\):表示从 ...

  8. docker-compose 快速部署Prometheus,监控docker 容器, 宿主机,ceph -- cluster集群

    话不多说上菜: 现在环境是这样: ceph 4台: 192.168.100.21  ceph-node1 192.168.100.22  ceph-node2 192.168.100.23  ceph ...

  9. PLCsim 软件模拟OB86故障

    用上一节 组态DP主站与标准从站的方法 组态了网络 实现了 将profibus –dp 标准从站 ET200M 下 输入地址为IW2 接口的状态 读取到 主机 DP-315-2DP 的QW0 变量以来 ...

  10. 51nod 1416:两点 深搜

    1416 两点 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 福克斯在玩一款手机解迷游戏,这个游戏叫做" ...