POJ - 2031C - Building a Space Station最小生成树
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 Output20.000
0.000
73.834
解题思路:题意:在一个三位平面上有几个球体,然后输入数据是给你N个球的球心坐标,以及半径。科学家们想要实现各个球之间的接触,也就是有表面的接触。
当然,两个球之间可能会有相交的地s(a,b) <= 0 ),那么这两个球是不用你新建道路来实现想通的,我们就可以把他们之间的距离设为0,然后再构建一个最小生成树就好,求最短路也一样,都可以解决~ 同时这道题也发现了一个细节吧。
数据位double类型的时候,
用G++的时候scanf要用%lf,而printf的时候要用%f,否则会WA! 代码如下:
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
using namespace std; int n ;
struct edge{
int u ;
int v ;
double w ;
}e[];
struct point{
double x;
double y;
double z;
double r;
}p[];
double get(point a , point b)
{
double ss = 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));
return ss;
}
bool cmp(edge a ,edge b)
{
return a.w<b.w;
}
double dis ;
int pre[];
int find(int x)
{
return (x==pre[x])?x:pre[x] = find(pre[x]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
dis = ;
int edge_num = ;
for(int i = ; i <= n ;i++)
{
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z,&p[i].r);
}
for(int i = ; i <= n ; i++)
{
pre[i] = i;
}
for(int i = ; i <= n ;i++)
{
for(int j = ; j <= n ;j++)
{
dis = get(p[i],p[j])-(p[i].r+p[j].r); e[edge_num].u = i ;
e[edge_num].v = j;
e[edge_num].w = dis<=0.000001?:dis;
edge_num++; }
}
sort(e,e+edge_num,cmp);
double ans = ;
int u , v ;
double w;
int fx , fy;
for(int i = ; i < edge_num ;i++)
{
u = e[i].u;
v = e[i].v;
w = e[i].w; fx = find(u);
fy = find(v);
if(fx!=fy)
{
pre[fx] = fy;
ans += w;
}
}
printf("%.3lf\n",ans);
}
return ;
}
POJ - 2031C - 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 最小生成树
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6083 Accepte ...
- POJ 2031 Building a Space Station
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- poj 2931 Building a Space Station <克鲁斯卡尔>
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5869 Accepted: 2 ...
- 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 ...
随机推荐
- 封装baseControl
package com.huawei.base; import java.io.IOException;import java.io.OutputStream;import java.io.Print ...
- 如何查看Mysql服务器上的版本
select version(); 1,mysql 的守护进程是mysqld [root@localhost ~]# service mysqld start 启动 MySQL: [确定] 你可以看看 ...
- web前端整套面试题(一)--js相关
一.单选 1.以下哪条语句会产生运行:(A) A.var obj = ( ); B.var obj = [ ]; C.var obj = { }; D.var obj = / /; B代表数组,C代表 ...
- 实现SwipeRefreshLayout首次进入自动刷新
看到了Android版知乎实现了这种效果,就自己也实现了一下. 先来一张效果图 实现方式: 方法一. ①在onWindowFocusChanged()方法中,设置为刷新状态为true @Overrid ...
- telnet客户端模拟浏览器发送请求
telnet 客户端 telnet客户端能够发出请求去连接服务器(模拟浏览器) 使用telnet之前,需要开启telnet客户端 1.进入控制面板 2.进入程序和功能,选择打开或关闭windows功能 ...
- Spring boot 开发组件
一.Jboot 描述:Jboot是一个基于jfinal 和 undertow开发的微服务框架.提供了AOP.RPC.分布式缓存.限流.降级.熔断.统一配置中心.swagger api自动生成.Open ...
- ssh 连接缓慢解决方法
ssh 连接缓慢解决方法 摘自:https://blog.csdn.net/qq_14821541/article/details/61915589 2017年03月13日 12:00:38 所以怎样 ...
- STM32F4通用定时器
1.基本原理 三种定时器区别 通用定时器功能特点描述 在这里只用输入捕获事件也能获取脉冲个数同时可以只使用它来获取脉冲宽度,比如当捕获到上升沿,马上进入中断,把计数器的值置零,然后等待捕获下降沿的到来 ...
- 1256 Anagram
题目链接: http://poj.org/problem?id=1256 题意: 根据自定义的字典序: 'A'<'a'<'B'<'b'<...<'Z'<'z' 和输 ...
- 特征工程 vs. 特征提取
“特征工程”这个华丽的术语,它以尽可能容易地使模型达到良好性能的方式,来确保你的预测因子被编码到模型中.例如,如果你有一个日期字段作为一个预测因子,并且它在周末与平日的响应上有着很大的不同,那么以这种 ...