POJ-2031 Building a Space Station (球的最小生成树)
http://poj.org/problem?id=2031
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
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 (球的最小生成树)的更多相关文章
- 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(计算几何+最小生成树)
http://poj.org/problem?id=2031 题意 给出三维坐标系下的n个球体,求把它们联通的最小代价. 分析 最小生成树加上一点计算几何.建图,若两球体原本有接触,则边权为0:否则边 ...
- POJ 2031 Building a Space Station (最小生成树)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5173 Accepte ...
- 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 三维球点生成树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
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- 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【最小生成树+简单计算几何】
You are a member of the space station engineering team, and are assigned a task in the construction ...
- 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 ...
- POJ 2031 Building a Space Station (计算几何+最小生成树)
题目: Description You are a member of the space station engineering team, and are assigned a task in t ...
随机推荐
- HDU 5480:Conturbatio 前缀和
Conturbatio Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- arm安装cuda9.0,tensorflow-gpu, jetson tx2安装Jetpack踩坑合集
因为要在arm(aarch64)架构的linux环境中安装tensorflow-gpu,但是官方tf网上没有对应的版本,所以我们找了好久,找到一个其他人编译好的tensorflow on arm的gi ...
- python脚本文件引用
二.Python __init__.py 作用详解 https://www.cnblogs.com/Lands-ljk/p/5880483.html __init__.py 文件的作用是将文件夹变为一 ...
- dns、网关、IP地址,主要是配置resolv.conf\network\ifcfg-eth0
Ubuntu sudo vi /etc/network/interfac 添加 dns-nameservers 192.168.1.254dns-search stonebean.com cent ...
- h5-伸缩布局
1.html代码 <div class="box"> <div class="first">1</div> <div ...
- C# 基本元素
一.构成C#的基本元素 注释和空白编译器不会编译,自动忽略:而标记是可以通过编译器编译的. 关键字 (keyword) 官方定义:关键字是类似标识符的保留的字符序列,不能用作标识符(以 @ 字符开头时 ...
- impdp导入.dmp到oracle
1.创建表空间 create tablespace CCGRP_PRO --表空间名 datafile 'D:\oracleData\test.dbf' --物理文件 表空间数据文件存放路径size ...
- mybatis的批量update
方法有三种:1.通过java代码batch方式,xml文件只需一条update语句.java代码繁琐 2.xml使用foreach,“;”分割多条update语句,要求:jdbc的url需加上allo ...
- .NET Core开发实战(第11课:文件配置提供程序)--学习笔记
11 | 文件配置提供程序:自由选择配置的格式 文件配置提供程序 Microsoft.Extensions.Configuration.Ini Microsoft.Extensions.Configu ...
- redis(七)---- SpringBoot和redis整合
SpringBoot和Redis整合非常简单 添加pom依赖 <dependency> <groupId>org.springframework.boot</groupI ...