http://poj.org/problem?id=2031

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

10.000 10.000 50.000 10.000
40.000 10.000 50.000 10.000
40.000 40.000 50.000 10.000 30.000 30.000 30.000 20.000
40.000 40.000 40.000 20.000 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

Sample Output

20.000
0.000
73.834

题意:

假设你是太空工作站的一员,现在安排你去完成一个任务:在N个小空间站(为球形)之间修路使他们能够互通,要求修路的长度要最小

如果两个空间站紧挨在一起,则不需要修路

多组数据输入

第一行  一个数字N 表示有N个子空间站

第2---N+1行   每行4个数据,前3个为空间站的球心的坐标x,y,z,第4个为半径r

思路:

很明显是最小生成树问题,关键是如何找到边关系

如果球之间相交,那么距离为0,否则求下

注意一些数据是double,别忘了

代码如下:

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
#define Bug cout<<"---------------------"<<endl
const int maxn=1e4+;
using namespace std; struct edge_node
{
int to;
double val;
int next;
}Edge[maxn*maxn/];
int Head[maxn];
int tot; struct point_node
{
double x,y,z;
double r;
}PT[maxn]; void Add_Edge(int u,int v,double w)
{
Edge[tot].to=v;
Edge[tot].val=w;
Edge[tot].next=Head[u];
Head[u]=tot++;
} double lowval[maxn];
int pre[maxn];//记录每个点的双亲是谁 double Prim(int n,int st)//n为顶点的个数,st为最小生成树的开始顶点
{
double sum=;
fill(lowval,lowval+n,INF);//不能用memset(lowval,INF,sizeof(lowval))
memset(pre,,sizeof(pre));
lowval[st]=-;
pre[st]=-;
for(int i=Head[st];i!=-;i=Edge[i].next)
{
int v=Edge[i].to;
double w=Edge[i].val;
lowval[v]=min(lowval[v],w);
pre[v]=st;
}
for(int i=;i<n-;i++)
{
double MIN=INF;
int k;
for(int i=;i<n;i++)//根据编号从0或是1开始,改i从0--n-1和1--n
{
if(lowval[i]!=-&&lowval[i]<MIN)
{
MIN=lowval[i];
k=i;
}
}
sum+=MIN;
lowval[k]=-;
for(int j=Head[k];j!=-;j=Edge[j].next)
{
int v=Edge[j].to;
double w=Edge[j].val;
if(w<lowval[v])
{
lowval[v]=w;
pre[v]=k;
}
}
}
return sum;
} int main()
{
int n;
while(~scanf("%d",&n)&&n!=)
{
memset(Head,-,sizeof(Head));
tot=;
for(int i=;i<n;i++)
{
scanf("%lf %lf %lf %lf",&PT[i].x,&PT[i].y,&PT[i].z,&PT[i].r);
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(i!=j)
{
double x,y,z;
x=PT[i].x-PT[j].x;
y=PT[i].y-PT[j].y;
z=PT[i].z-PT[j].z;
double val=sqrt(x*x+y*y+z*z)-PT[i].r-PT[j].r;//球心距离减去两个半径的距离
if(val>)
Add_Edge(i,j,val);
else
Add_Edge(i,j,);
}
}
}
printf("%.3f\n",Prim(n,));
}
return ;
}

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

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

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

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

    http://poj.org/problem?id=2031 题意 给出三维坐标系下的n个球体,求把它们联通的最小代价. 分析 最小生成树加上一点计算几何.建图,若两球体原本有接触,则边权为0:否则边 ...

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

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

  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 三维球点生成树Kruskal

    Building a Space Station You are a member of the space station engineering team, and are assigned a ...

  6. POJ 2031 Building a Space Station

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

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

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

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

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

  9. POJ 2031 Building a Space Station (prim裸题)

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

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

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

随机推荐

  1. 13 ~ express ~ 后台页面的搭建

    一, 后台路由文件  /router/admin.js  var express = require('express') var router = express.Router() /** * 验证 ...

  2. prometheus配置简介

    参考网页:https://my.oschina.net/wangyunlong/blog/3060776 global: scrape_interval:             15s evalua ...

  3. C++ STD Gems06

    generate.generate_n.sample.iota #include <iostream> #include <vector> #include <strin ...

  4. 二分+半平面交——poj1279

    /* 二分距离,凸包所有边往左平移这个距离,半平面交后看是否还有核存在 */ #include<iostream> #include<cstring> #include< ...

  5. 实现VR直播的关键技术

    VR是多媒体技术发展的必然趋势,人们所使用的信息载体从最早的文字.图像,到音视频,再到用VR,将事物的描述表达推向了极致,充分满足了沉浸性.互动性和构想性三大要素的需求.随着5G的商业化运营,VR有望 ...

  6. centos 7.4 安装docker 19.03.6 版本。附带离线安装包

    说明: 1.此环境为未安装过docker服务的环境, 如果已经安装,则自行卸载. 2.以下环境中上传的包及离线yum源默认为/home目录下,如无特殊说明,以此目录为准 步骤一:下载docker离线安 ...

  7. 吴裕雄--天生自然 JAVASCRIPT开发学习:运算符

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. idea xml文件去掉背景黄色

    编写dao中的sql时,xml文件中背景一大片黄色,看着不舒服,如何去掉了? 1. File -> Settings... 2. 去消以下两项勾选    (Inspections    -- 如 ...

  9. Java基础查漏补缺(1)

    Java基础查漏补缺 String str2 = "hello"; String str3 = "hello"; System.out.println(str3 ...

  10. js 数据

    非0数字值 都是true  0和NaN 都是false 任何对象   都是true  null       都是false undefined 不适用 详情