POJ 2031 Building a Space Station (最小生成树)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 5173 | Accepted: 2614 |
Description
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
n
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
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
判断两球表面距离是否小于零,即球心距 - 半径1 - 半径2 <= 0,如果满足的话就合并掉,不改变答案值,然后跑一边kruskal就行。
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
int FATHER[SIZE],N,NUM;
struct Node
{
int from,to;
double cost;
}G[SIZE * SIZE];
struct
{
double x,y,z,r;
}TEMP[SIZE]; void ini(void);
int find_father(int);
void unite(int,int);
bool same(int,int);
bool comp(const Node &,const Node &);
double kruskal(void);
double dis(double,double,double,double,double,double);
int main(void)
{
while(~scanf("%d",&N))
{
if(!N)
break;
ini();
for(int i = ;i <= N;i ++)
scanf("%lf%lf%lf%lf",&TEMP[i].x,&TEMP[i].y,&TEMP[i].z,&TEMP[i].r);
for(int i = ;i <= N;i ++)
for(int j = i + ;j <= N;j ++)
{
G[NUM].from = i;
G[NUM].to = j;
G[NUM].cost = dis(TEMP[i].x,TEMP[i].y,TEMP[i].z,TEMP[j].x,TEMP[j].y,TEMP[j].z)
- TEMP[i].r - TEMP[j].r;
if(G[NUM].cost <= )
unite(i,j);
NUM ++;
}
sort(G,G + NUM,comp);
printf("%.3f\n",kruskal());
} return ;
} void ini(void)
{
NUM = ;
for(int i = ;i <= N;i ++)
FATHER[i] = i;
} int find_father(int n)
{
if(n == FATHER[n])
return n;
return FATHER[n] = find_father(FATHER[n]);
} void unite(int x,int y)
{
x = find_father(x);
y = find_father(y); if(x == y)
return ;
FATHER[x] = y;
} bool same(int x,int y)
{
return find_father(x) == find_father(y);
} bool comp(const Node & a,const Node & b)
{
return a.cost < b.cost;
} double kruskal(void)
{
double ans = ; for(int i = ;i < NUM;i ++)
if(!same(G[i].from,G[i].to))
{
unite(G[i].from,G[i].to);
ans += G[i].cost;
}
return ans;
} double dis(double x_1,double y_1,double z_1,double x_2,double y_2,double z_2)
{
return sqrt(pow(x_1 - x_2,) + pow(y_1 - y_2,) + pow(z_1 - z_2,));
}
POJ 2031 Building a Space Station (最小生成树)的更多相关文章
- POJ 2031 Building a Space Station 最小生成树模板
题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...
- POJ 2031 Building a Space Station【经典最小生成树】
链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- POJ 2031 Building a Space Station (最小生成树)
Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...
- poj 2031 Building a Space Station【最小生成树prime】【模板题】
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5699 Accepte ...
- POJ 2031 Building a Space Station
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- 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 ...
- POJ 2031 Building a Space Station (计算几何+最小生成树)
题目: Description You are a member of the space station engineering team, and are assigned a task in t ...
- POJ 2031 Building a Space Station【最小生成树+简单计算几何】
You are a member of the space station engineering team, and are assigned a task in the construction ...
- POJ - 2031C - Building a Space Station最小生成树
You are a member of the space station engineering team, and are assigned a task in the construction ...
随机推荐
- 【css hack】正是我所找的,帮了大忙啊
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-03-05) 各个浏览器单独设置属性 IE6:能识别下划线 “_” 和 星号 “*“,不能识别 “!important” ...
- Linq使用Group By经验总结
1.计数 var q = from p in db.Products group p by p.CategoryID into g select new { g.Key, NumProducts = ...
- JobScheduler
任务写在JobService中 public class TestJobService extends JobService { private static final String TAG = & ...
- iOS 蒙板,图片叠加显示漏空部分
懒惰了一个月了,今天写写项目里遇到的一个问题. 图片a 和图片b相互叠加,a图片四周是白色的不规则图形,里面填充黑色. b图片是一张正常图片. 需求是叠加在一起,要求将b图片根据a图片的黑色形状 扣出 ...
- C# 实现对网站数据的采集和抓取
首先大家需要清楚一点的是:任何网站的页面,无论是php.jsp.aspx这些动态页面还是用后台程序生成的静态页面都是可以在浏览器中查看其HTML源文件的. 所以当你要开发数据采集程序的时候,你必须先对 ...
- codeforces Gym 100187F F - Doomsday 区间覆盖贪心
F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...
- 搭建Spring + SpringMVC + Mybatis框架之三(整合Spring、Mybatis和Spring MVC)
整合Spring和SpringMVC 之前已经整合了spring和mybatis,现在在此基础上整合SSM. 项目目录: 思路:SpringMVC的配置文件独立,然后在web.xml中配置整合. (1 ...
- Spark1.0.0 开发环境高速搭建
在本系列博客中.为了解析一些概念.解析一些架构.代码測试.搭建了一个实验平台.例如以下图所看到的: 本实验平台是在一台物理机上搭建的.物理机的配置是16G内存,4核8线程CPU ...
- Android平台上长连接的实现
Android 平台上长连接的实现 为了不让 NAT 表失效,我们需要定时的发心跳,以刷新 NAT 表项,避免被淘汰. Android 上定时运行任务常用的方法有2种,一种方法用 Timer,另一种是 ...
- 技术随笔 查找速度最快的Google IP
转:http://www.xiumu.org/technology/the-find-the-fastest-in-the-google-ip.shtml 体验秒开GOOGLE的感觉! 在http:/ ...