POJ 2031:Building a Space Station 最小生成树
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 6083 | Accepted: 3024 |
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
题意是给了一堆三维坐标下的空间站,空间站都是球形,有其球心坐标和半径。问把这些空间站都连接起来,问最小代价。
就是求最小生成树,两个空间站T1 T2的距离是坐标下的距离 - T1的半径 - T2的半径
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct no{
double x;
double y;
double z;
double r;
}node[105]; int num,n;
double map[102][102];
int stack[102];
double minidis[102]; double prim()
{
int i,j,s;
double result; memset(stack,0,sizeof(stack));
for(i=1;i<=num;i++)
{
minidis[i]=100000005;
} stack[1]=1;
minidis[1]=0;
s=1;
result=0; for(i=1;i<=num-1;i++)
{
double min_all=100000005;
int min_temp=0;
for(j=2;j<=num;j++)
{
if(stack[j]==0&&minidis[j]>map[s][j])
{
minidis[j]=map[s][j];
}
if(stack[j]==0&&minidis[j]<min_all)
{
min_temp=j;
min_all=minidis[j];
}
}
s=min_temp;
stack[s]=1;
result += min_all;
}
return result;
} int main()
{
int i,j;
while(cin>>n)
{
if(n==0)
break;
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
cin>>node[i].x>>node[i].y>>node[i].z>>node[i].r;
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
map[j][i]=map[i][j]=max(0.0,sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x) +(node[i].y-node[j].y)*(node[i].y-node[j].y) +(node[i].z-node[j].z)*(node[i].z-node[j].z))-node[i].r-node[j].r);
}
}
num=n;
printf("%.3lf\n",prim());
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2031:Building a Space Station 最小生成树的更多相关文章
- 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 最小生成树模板
题目大意:在三维坐标中给出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 ...
随机推荐
- Caused by: java.net.ConnectException: Connection timed out: connect
发生这种情况的原因是:连接的路径发生错误
- Kubernetes 二进制部署(一)单节点部署(Master 与 Node 同一机器)
0. 前言 最近受“新冠肺炎”疫情影响,在家等着,入职暂时延后,在家里办公和学习 尝试通过源码编译二进制的方式在单一节点(Master 与 Node 部署在同一个机器上)上部署一个 k8s 环境,整理 ...
- JavaWeb开发:从购买服务器到简单demo运行
写这篇文章的目的: 一个是为了记录实施过程,方便自己日后查阅: 另一个是给项目组成员提供一个参考,方便他们以后搭建自己的项目环境: 当然若能帮助到更多的朋友,那就再好不过了:D 需要注意: 我本身也是 ...
- 1. GC标记-清除算法(Mark Sweep GC)
世界上第一个GC算法,由 JohnMcCarthy 在1960年发布. 标记-清除算法由标记阶段和清除阶段构成. 标记阶段就是把所有的活动对象都做上标记的阶段. 标记阶段就是"遍历对象并标记 ...
- Redis官方Tutorial
基本命令 包括SET , GET , INCR , DEL , EXPIRE , TTL SET server:name "10" GET server:name IN ...
- k8s node断电重启
kubernetes断电重启 导致部分pod无法删除 dashboard上处于黄色 kubectl get处于terminate 状态 kubectl delete报错: An error occur ...
- 第1节 IMPALA:4、5、linux磁盘的挂载和上传压缩包并解压
第二步:开机之后进行磁盘挂载 分区,格式化,挂载新磁盘 磁盘挂载 df -lh fdisk -l 开始分区 fdisk /dev/sdb 这个命令执行后依次输 n p 1 回车 回车 w ...
- Visual Studio中的“build”、“rebuild”、“clean”的区别
区别 rebuild基本相当于clean+build build只针对修改过的文件进行编译,rebuild会对所有文件编译(无论是否修改). clean 删除中间和输出文件,中间文件是指一些生成应用的 ...
- express - 快速构建项目
1,cnpm i express -g 2, cnpm i express-generater -g 3, express - e 项目名
- torch.nn.Conv2d()使用
API 输入:[ batch_size, channels, height_1, width_1 ] Conv2d输入参数:[ channels, output, height_2, width_2 ...