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

时间:2022-08-31 16:33:36

链接:



Building a Space Station
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3578   Accepted: 1830

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

[Submit]   [Go Back]   [Status]  
[Discuss]


 

题意:

在三维空间中给你 n 个球体的坐标和半径

    如果这些球体间没相通,则需要你去建立一些通道把所有的球体连接起来。

    表面相切即可认为相通。

算法:


最小生成树 Kruskal 复杂度O(ElogE) 排序很快logE可以忽略 直接是O(E)了



或者

最小生成树 Prime  复杂度O(n*n)

思路:


      经典最小生成树题目
Kruskal:

   首先在各球体间建图,然后再按照边从小到大排序

   用并查集查找两点是否属于同一联通分量【即判断这条边的两个球是否相通】

   如果不属于同一联通分量,则连接即可

   由于每次都是找的最短的边,所以最终所求一定是最短距离了。
Prime:
  从第一个点开始加入空的连通分量,

  再找第一个点距离最近的点,

  这个时候连同分量中就有了两个点。

  然后再不停的找不在这一连通分量中的距离连通分量最近的一个点

  直到所有的点都加入了这一连通分量 

  由于每次都是找的【距离连通分量整体】最近的点,所以结果必然是最优的了
下面推荐的学习最小生成树的,也是属于Prime了,数据结构课上老师讲的。


相关算法学习:

lrj《白书》 P200-P201

最小生成树:

http://blog.csdn.net/cfreezhan/article/details/8189218

并查集:

http://blog.csdn.net/cfreezhan/article/details/8629871

http://blog.csdn.net/cfreezhan/article/category/1219856

Kruskal

/****************************************************
Accepted 248 KB 32 ms C++ 1885 B 2013-07-26 15:47:09
题意:在三维空间中给你 n 个球体的坐标和半径
如果这些球体间没有相通,则需要你去建立一些通道把所有的球体连接起来。
表面相切即可认为相通。 算法:最小生成树Kruskal 复杂度 O(E) 思路:经典最小生成树题目
首先在各球体间建图,然后再按照边从小到大排序
用并查集查找两点是否属于同一联通分量【即判断这条边的两个球是否相通】
如果不属于同一联通分量,则连接即可
由于每次都是找的最短的边,所以最终所求一定是最短距离了。
****************************************************/
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = 110;
int n,m; struct Point{
double x,y,z;
double r;
}p[maxn];
int f[maxn]; /**父亲*/ struct Edge{
int u,v;
double w;
}edge[maxn*maxn]; bool cmp(Edge L1, Edge L2)
{
return L1.w < L2.w;
} double dist(Point A, Point B)
{
return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y) + (A.z-B.z)*(A.z-B.z));
} int find(int x) /** 并查集find*/
{
return x == f[x] ? x : f[x] = find(f[x]);
} double Kruskal() /** 传说中的 Kruskal 算法, 学并查集时居然没有看到Orz*/
{
double ans = 0;
for(int i = 0; i < m; i++) /*排序后遍历的边一定是从小到大的*/
{
int u = find(edge[i].u); /**找祖宗*/
int v = find(edge[i].v); if(u == v) continue; /**祖宗相同, 属于同一连通分量*/
else /**属于不同联通分量, 合并*/
{
ans += edge[i].w;
f[u] = v;
}
}
return ans;
}
int main()
{
while(scanf("%d", &n) != EOF)
{
if(n == 0) break; for(int i = 0; i < n; i++)
{
scanf("%lf%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z, &p[i].r);
f[i] = i; /** 初始化并查集,自己是自己的祖宗*/
} m = 0; /** 初始化边的数量*/
for(int i = 0; i < n-1; i++)
{
for(int j = i+1; j < n; j++)
{
edge[m].u = i;
edge[m].v = j;
edge[m].w = max(0.0, dist(p[i],p[j])-p[i].r-p[j].r); /**如果两个圆相交,则定义距离为 0 */
m++;
}
}
sort(edge,edge+m,cmp); /** 把边按照长度从小到大排序 */ double ans = Kruskal();
printf("%.3lf\n", ans);
}
return 0;
}

Prime

/****************************************************
Accepted 252 KB 16 ms C++ 1419 B 2013-07-26 21:32:40
题意:在三维空间中给你 n 个球体的坐标和半径
如果这些球体间没有相通,则需要你去建立一些通道把所有的球体连接起来。
表面相切即可认为相通。 算法:最小生成树 Prime 复杂度 O(n*n) 思路:经典最小生成树题目
首先在各球体间建图,
从第一个点开始加入空的连通分量,
再找第一个点距离最近的点,
这个时候连同分量中就有了两个点。
然后再不停的找不在这一连通分量中的距离连通分量最近的一个点
直到所有的点都加入了这一连通分量
由于每次都是找的【距离连通分量整体】最近的点,所以结果必然是最优的了
****************************************************/
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 110;
const double DNF = 3000; double w[maxn][maxn];
double d[maxn];
int vis[maxn];
int n; struct Point{
double x,y,z;
double r;
}p[maxn]; double dist(Point A, Point B)
{
return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y) + (A.z-B.z)*(A.z-B.z));
} double Prime()
{
double ans = 0;
for(int i = 0; i < n; i++) d[i] = DNF;
d[0] = 0; /** 第一个点入连通分量*/ memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; i++)
{
int x;
double m = DNF; /** 不断的找下一个距离连通分量最小的点*/
for(int y = 0; y < n; y++) if(!vis[y] && d[y] <= m) m = d[x=y];
vis[x] = 1; /** 标记进入连通分量*/
ans += d[x]; /** 加入总路径 */
for(int y = 0; y < n; y++) if(!vis[y]) /**不断更新剩下未加入连通分量的点与连通分量的最短距离*/
d[y] = min(d[y], w[x][y]);
}
return ans;
} int main()
{
while(scanf("%d", &n) != EOF)
{
if(n == 0) break; for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
w[i][j] = DNF; for(int i = 0; i < n; i++)
scanf("%lf%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z, &p[i].r); for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++)
{
if(i == j) w[i][j] = 0;
else w[i][j] = max(0.0, dist(p[i], p[j])-p[i].r-p[j].r);
w[j][i] = w[i][j];
}
}
printf("%.3lf\n", Prime());
}
return 0;
}

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 &lpar;计算几何&plus;最小生成树&rpar;

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

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

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

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

  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. Java—网络技术

    1  TCP Sockets基础 Sockets是一个编程抽象概念,它是网络上与另一个应用程序通信连接的句柄.Sockets编程将用户代码与TCP/IP协议堆栈的底层实现隔离开,允许用户灵活地实现自己 ...

  2. IE9父容器overflow&colon;auto时,子容器状态更改导致滚动条下出现额外空间的问题探讨

    IE的每次跟新都会有一些奇葩的bug,我们默默承受了. 这个问题在项目中出现困扰了我近一个星期,这里记录一下.看下面实例 <style> .panel{ width: 200px; ove ...

  3. mesos 学习笔记1 -- mesos安装和配置

    参考资料: 官方文档:http://mesos.apache.org/documentation 中文翻译:http://mesos.mydoc.io/ GitHub:https://github.c ...

  4. 关于JS闭包,作者不详(转)

    说明:本文由两篇文章结合而成,系从他人笔记中转过来的, 具体作者不详.因为觉得不错,遂共享之.如有侵权,立删致歉. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变 ...

  5. 【ASP&period;NET 进阶】根据IP地址进行百度地图定位

    昨天有完成一个[ASP.NET 进阶]根据IP返回对应位置信息 的小Demo,既然可以通过IP获得位置信息,那当然可以通过位置信息的经纬度获取IP的当前定位了,虽然与实际地址偏移较大,毕竟不是GPRS ...

  6. 卷积神经网络CNN的原理(三)---代码解析

    卷积神经网络在几个主流的神经网络开源架构上面都有实现,我这里不是想实现一个自己的架构,主要是通过分析一个最简单的卷积神经网络实现代码,来达到进一步的加深理解卷积神经网络的目的. 笔者在github上找 ...

  7. Drools 规则引擎

    Drools - Drools - Business Rules Management System (Java™, Open Source) http://drools.org/ [Drools]J ...

  8. Asterisk1&period;8 sip编码协商分析

    在开始分析之前,先对编码协商中可能涉及的asterisk数据结构和变量作些说明.ast_channel:定义一个通用的通道数据结构 struct ast_channel { const struct ...

  9. 遇到can not resolve app 依赖包的问题

    1.第一种解决方式:build->sdkmannger->sdk tool ->安装android support responsitory和google responsitory ...

  10. 栈溢出笔记1&period;3 准备Shellcode

    经过1.1和1.2节的讲述,我们已经知道了怎样更改EIP的值. 程序运行函数之后将跳转到我们设定的位置開始运行,因此,我们须要准备一个自己的程序,接手后面的工作.这是一个什么样的程序?是一个C语言编写 ...