题目:

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

题意:
给出n个圆 如果两圆相交 则两圆间距离为0 求最小生成树 思路:
先用计算几何内容求出任意两圆间距离 然后连边 如果两圆相交 mp[i][j]=0 如果两圆不相交 mp[i][j]=两圆距离-两圆半径和
处理成矩阵图 用prim算法跑最小生成树

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=;
const double eps=1e-;
int n;
int vis[maxn];
double mp[maxn][maxn],dis[maxn]; int dps(double x){
if(fabs(x)<eps) return ;
return x>?:-;
} struct Point{
double x,y,z,r;
}kk[maxn]; double len(Point a,Point b){
return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y)+(b.z-a.z)*(b.z-a.z));
} int main(){
while(~scanf("%d",&n)){
if(n==) break;
memset(mp,,sizeof(mp));
memset(dis,,sizeof(dis));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&kk[i].x,&kk[i].y,&kk[i].z,&kk[i].r);
}
for(int i=;i<=n;i++){
dis[i]=(double)inf;
for(int j=i+;j<=n;j++){
double tmp=len(kk[i],kk[j]);
double tmp2=kk[i].r+kk[j].r;
if(dps(tmp-tmp2)<=) mp[i][j]=mp[j][i]=0.0;
else mp[i][j]=mp[j][i]=tmp-tmp2;
}
}
for(int i=;i<=n;i++){
dis[i]=mp[][i];
}
dis[]=0.0;
vis[]=;
double sum=;
int tmp;
for(int i=;i<=n;i++){
tmp=inf;
double minn=(double)inf;
for(int j=;j<=n;j++){
if(vis[j]== && dis[j]<minn){
tmp=j;
minn=dis[j];
}
}
if(tmp==inf) break;
vis[tmp]=;
sum+=minn;
for(int j=;j<=n;j++){
if(vis[j]== && dis[j]>mp[tmp][j])
dis[j]=mp[tmp][j];
}
}
printf("%.3f\n",sum);
} return ;
}

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

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

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

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

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

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

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

  4. poj 2031 Building a Space Station(最小生成树,三维,基础)

    只是坐标变成三维得了,而且要减去两边的半径而已 题目 //最小生成树,只是变成三维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> ...

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

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

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

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

  7. POJ 2031 Building a Space Station

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

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

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

随机推荐

  1. mysql 提高一 动态sql 传变量

    1.需求 DELIMITER $$ SECOND STARTS '2018-09-07 08:00:00' ON COMPLETION PRESERVE ENABLE DO BEGIN ) DEFAU ...

  2. 内存管理中提到的hot cold page

    所谓冷热是针对处理器cache来说的,冷就是页不大可能在cache中,热就是有很大几率在cache中. cold page和hot page的概念可以参考LWN的一片文章http://lwn.net/ ...

  3. union的特性,去重与不去重

    转载:https://blog.csdn.net/kingmax54212008/article/details/33762921 union的特性,去重与不去重 集合操作有 并,交,差 3种运算. ...

  4. Django_modelform组件

    modelForm 组件 概念 将数据库与form 组件结合用起来的中间插件 与 form 组件的区别 form组件的难处: form 可以实现 对数据的验证以及 form 的表单标签的生成 但是她做 ...

  5. Word自定义多级列表样式

    Word自定义多级列表样式: 1. 2. 3.取个名字 在这里鼠标移上时显示 : 4. 5. 定义完成,即可使用:

  6. Linux squid代理

    代理的作用: 共享网络 : 加快访问速度,节约通信带宽 : 防止内部主机受到攻击 : 限制用户访问,完善网络管理: 标准代理: 首先要在内部主机指定代理服务器的IP和port,然后通过代理服务器访问外 ...

  7. 【Spring】Spring bean的实例化

    Spring实现HelloWord 前提: 1.已经在工程中定义了Spring配置文件beans.xml 2.写好了一个测试类HelloWorld,里面有方法getMessage()用于输出" ...

  8. 【并发编程】【JDK源码】JDK的(J.U.C)java.util.concurrent包结构

    本文从JDK源码包中截取出concurrent包的所有类,对该包整体结构进行一个概述. 在JDK1.5之前,Java中要进行并发编程时,通常需要由程序员独立完成代码实现.当然也有一些开源的框架提供了这 ...

  9. shell之数学运算

    let #!/bin/bash no1=1; no2=5; let result=no1+no2 ##不能留空格 echo $result #自加 let no++ #自减 let no-- #简写 ...

  10. Activation HDU - 4089(概率dp)

    After 4 years' waiting, the game "Chinese Paladin 5" finally comes out. Tomato is a crazy ...