POJ2031 Building a Space Station 2017-04-13 11:38 48人阅读 评论(0) 收藏

时间:2022-08-31 15:37:21
Building a Space Station
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8572   Accepted: 4093

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

Source

—————————————————————————————————————

题目的意思是在一个三维的空间中给出n个球体,球的球心和半径已知,现在要将所有的球连通起来,两个球的花费是圆心距减去半径和(接触为0),问最小花费

思路:把每个球当做一个点两两建边,求最小生成树

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
using namespace std;
#define LL long long struct node
{
int u,v;
double w;
} p[100005];
int n,cnt,pre[106]; bool cmp(node a,node b)
{
return a.w<b.w;
}
void init()
{
for(int i=0; i<105; i++)
pre[i]=i;
} int fin(int x)
{
return pre[x]==x?x:pre[x]=fin(pre[x]);
} void kruskal()
{
sort(p,p+cnt,cmp);
init();
double cost=0;
int ans=0;
for(int i=0; i<cnt; i++)
{
int a=fin(p[i].u);
int b=fin(p[i].v);
if(a!=b)
{
pre[a]=b;
cost+=p[i].w;
ans++;
}
if(ans==n-1)
{
break;
}
}
printf("%.3f\n",cost);
} int main()
{
double x[105],y[105],z[105],r[105];
while(~scanf("%d",&n)&&n)
{ for(int i=0; i<n; i++)
scanf("%lf%lf%lf%lf",&x[i],&y[i],&z[i],&r[i]);
cnt=0;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
{
p[cnt].u=i,p[cnt].v=j;
p[cnt++].w=max(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+(z[i]-z[j])*(z[i]-z[j]))-r[i]-r[j],0.0);
}
kruskal();
}
return 0;
}

POJ2031 Building a Space Station 2017-04-13 11:38 48人阅读 评论(0) 收藏的更多相关文章

  1. HDU1241 Oil Deposits 2016-07-24 13&colon;38 66人阅读 评论&lpar;0&rpar; 收藏

    Oil Deposits Problem Description The GeoSurvComp geologic survey company is responsible for detectin ...

  2. POJ1087 A Plug for UNIX 2017-02-12 13&colon;38 40人阅读 评论&lpar;0&rpar; 收藏

    A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeting ...

  3. 百度地图-省市县联动加载地图 分类: Demo JavaScript 2015-04-26 13&colon;08 530人阅读 评论&lpar;0&rpar; 收藏

    在平常项目中,我们会遇到这样的业务场景: 客户希望把自己的门店绘制在百度地图上,通过省.市.区的选择,然后加载不同区域下的店铺位置. 先看看效果图吧: 实现思路: 第一步:整理行政区域表: 要实现通过 ...

  4. Segment Tree 分类: ACM TYPE 2014-08-29 13&colon;04 97人阅读 评论&lpar;0&rpar; 收藏

    #include<iostream> #include<cstdio> using namespace std; struct node { int l, r, m; int ...

  5. HDU1349 Minimum Inversion Number 2016-09-15 13&colon;04 75人阅读 评论&lpar;0&rpar; 收藏

    B - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  6. HDU6024 Building Shops 2017-05-07 18&colon;33 30人阅读 评论&lpar;0&rpar; 收藏

    Building Shops                                                             Time Limit: 2000/1000 MS ...

  7. Power Network 分类: POJ 2015-07-29 13&colon;55 3人阅读 评论&lpar;0&rpar; 收藏

    Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24867 Accepted: 12958 Descr ...

  8. Oracle错误IMP-00010&colon; 不是有效的导出文件&comma; 头部验证失败 分类: Oracle 2015-07-09 13&colon;56 20人阅读 评论&lpar;0&rpar; 收藏

    Oracle 11g的dmp备份文件导入到Oracle 10g,出现错误信息: Import: Release 10.2.0.1.0 - Production on 星期四 7月 9 13:47:04 ...

  9. Eclipse 快捷键大全 分类: C&lowbar;OHTERS 2014-06-01 13&colon;05 332人阅读 评论&lpar;0&rpar; 收藏

      精选常用: 1.  ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如a ...

随机推荐

  1. Memcached 在windows环境下安装

    1.memcached简介 memcached是一个高性能的分布式内存对象缓存系统,它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动应用的访问性 能.memcached基于 ...

  2. 对GBK的理解(内附全部字符编码列表):扩充的2万汉字低字节的高位不等于1,而且还剩许多编码空间没有利用

    各种编码查询表:http://bm.kdd.cc/ 由于GB 2312-80只收录6763个汉字,有不少汉字,如部分在GB 2312-80推出以后才简化的汉字(如“啰”),部分人名用字(如中国前总理朱 ...

  3. SQL语句操作大全

    SQL语句操作大全   本文分为以下六个部分: 基础部分 提升部分 技巧部分 数据开发–经典部分 SQL Server基本函数部分 常识部分 一.基础 1.说明:创建数据库CREATE DATABAS ...

  4. RAILS ON

    我是按照下面这个URL来轻快安装的. http://lxiaodao.iteye.com/blog/1579992 (1)RVM官方网站应该是改版过一次, 使用 curl -L https://get ...

  5. php安装pear、pecl

    安装pear.pecl特别简单,只需要两步. wget http://pear.php.net/go-pear.phar php go-pear.phar [root@localhost bin]# ...

  6. R-CNN论文翻译

    R-CNN论文翻译 Rich feature hierarchies for accurate object detection and semantic segmentation 用于精确物体定位和 ...

  7. N卡全部历史驱动

    记录一下寻找驱动方法 打开链接 http://www.geforce.cn/drivers/beta-legacy 默认搜索出来是10个,之后打开控制台输入如下内容回车显示全部(100,可以修改数字来 ...

  8. 测试BUG记录模板(供参考)

    文档说明如下: Bug严重程度: A-崩溃的:由于程序所引起的死机.非法退出.死循环:数据库发生死锁:因错误操作导致的程序中断:主要功能错误:造成数据破坏丢失或数据异常:数据库连接错误:数据通讯错误. ...

  9. &lbrack;Swift&rsqb;LeetCode541&period; 反转字符串 II &vert; Reverse String II

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  10. linux-shell系列6-rundeck生成host文件

    nmap -sP 192.168.30.* -PS22 -oG nmap.out && awk '/Status: Up/ {print $2}' nmap.out > /tmp ...