POJ 2031:Building a Space Station 最小生成树

时间:2022-08-31 15:37:27
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 &lpar;最小生成树&rpar;

    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&Tab; 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 &lpar;计算几何&plus;最小生成树&rpar;

    题目: 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【最小生成树&plus;简单计算几何】

    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. XAML Region标签功能

    XAML本身没有Region标签功能,很郁闷.现在有插件可以实现该功能了!   <!-- Region (Any Text You Want) --> Your Code <!-- ...

  2. 【微信开发】 新浪SAE开发平台 注意事项

    1. 微信开发 新浪SAE开发平台 验证Token 一直失败? 这个问题困扰了一个又一个的微信学习者,现在百度到的答案有:在echo $echoStr;之前添加header('content-type ...

  3. c&num;中的partial关键字&lpar;转&rpar;

    c#中的partial关键字 Partial:      这是C#2.0的新特性 Partial关键字起到把一个class分段组合作用,能用于多个接口或一个继承 代码片断: namespace Com ...

  4. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  5. abap优化工具事务代码: ST05

  6. androidpn-server笔记及BUG修改

    上篇讲了androidpn的client端,这篇该讲一下我使用androidpn-server端的笔记了. 这里我使用的androidpn是tomcat版的,由不知哪位大神移植并修复了部分bug的版本 ...

  7. ASP&period;NET MVC4中加入Log4Net日志记录功能

    前言 在之前的.NET中,微软还没有提供过像样的日志框架,目前能用的一些框架比如Log4Net.NLog.CommonLogging等,虽然多多少少使用起来有点费劲,但这里还是简单分享一下Log4Ne ...

  8. 使用Puppeteer进行数据抓取&lpar;四&rpar;——快速调试

    在我们使用chrome作为爬虫获取网页数据时,往往需如下几步. 打开chrome 导航至目标页面 等待目标页面加载完成 解析目标页面数据 保存目标页面数据 关闭chrome 我们实际的编码往往集中在第 ...

  9. 20145101《Java程序设计》第10周学习总结

    20145101<Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输. 计算机网络 路由器和交换机组成了核心的 ...

  10. 【bzoj1194】 HNOI2006—潘多拉的盒子

    http://www.lydsy.com/JudgeOnline/problem.php?id=1194 (题目链接) 题意 给出S个自动机,如果一个自动机u的所有状态是另一个自动机v的状态的子集,那 ...