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

时间:2022-08-31 15:37:15

题目:

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

    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 &lpar;最小生成树&rpar;

    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 &lpar;prim裸题&rpar;

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

随机推荐

  1. CYQ&period;Data V5 分布式缓存MemCached应用开发介绍

    前言 今天大伙还在热议关于.NET Core的东西,我只想说一句:在.NET 跨平台叫了这么多年间,其实人们期待的是一个知名的跨平台案例,而不是一堆能跨平台的消息. 好,回头说说框架: 在框架完成数据 ...

  2. Android Studio导入Vitamio多媒体开发框架

    PS:这篇笔记用于解决Android Studio导入Vitamio框架的问题.官网给出的相关说明过于简单,故整理这篇文章,希望能帮助到像我一样遇到这个问题的朋友. 开发学习建议参考农民伯伯的博客中的 ...

  3. shell脚本批量调用git命令

    有时候想对本地的几个repository都进行一下pull,一个一个操作比较繁琐,所以写了个shell脚本进行简化操作. git_pull_all.sh #!/bin/sh clear functio ...

  4. log4j配置不同的类多个日志文件

    <Configuration status="INFO"> <Appenders> <Console name="STDOUT" ...

  5. Android WebView的loadData方法注意事项

    loadData()中的html data中不能包含'#', '%', '\', '?'四中特殊字符,出现这种字符就会出现解析错误,显示找不到网页还有部分html代码.需要如何处理呢?我们需要用Url ...

  6. Sereja and Coat Rack&lpar;水&rpar;

    Sereja and Coat Rack Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  7. oracle temporary table

    oralce 有两种临时表  a.会话级临时表 b.事物级临时表 A.事物级临时表 语法 create  global temporary table table_name( col1  type1, ...

  8. cannot open shared object file&colon; No such file or directory

    一般我们在Linux下执行某些外部程序的时候可能会提示找不到共享库的错误, 比如:error while loading shared libraries: libxxx.so: cannot ope ...

  9. canvas&plus;js实现荧光字符效果

    一个小玩意,代码来源于网络. 效果图如下 代码如下 <html> <head> <style> * { margin: 0; padding: 0; } html, ...

  10. chrome 插件学习笔记(一)

    主要是屏蔽cnbeta中屏蔽广告之后的弹出层 manifest.json文件 { "js": ["jquery-1.7.2.min.js","cnbe ...