POJ2031 Building a Space Station【最小生成树】
题意:
就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通。如果两个球有重叠的部分则算为已连通,无需再搭桥。求搭建通路的最小边长总和是多少。
思路:
先处理空间点之间的距离,要注意的是两个球面相交的情况,相交的话距离是0。两球面距离是球心距离减去两个球的半径(边权 = AB球面距离 = A球心到B球心的距离 – A球半径 – B球半径),相交时距离是算出来是负值,要改为0。其实就是求连接所有球最小生成树的过程。
代码:
kruskal:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std; int n;
double need; struct Station //定义球的结构体
{
double x,y,z;
double r;
};
Station station[]; struct Vdge //构造生成树的点
{
int x,y;
double l;
}; bool cmp(Vdge a,Vdge b){
return a.l < b.l ;
} Vdge edge[];
int road[]; int find(int x)
{
while( x != road[x] )
x = road[x];
return x;
} bool judge(int x,int y){
int fx = find(x);
int fy = find(y);
if( fx==fy )
return false;
else
{
road[fx] = fy;
return true;
}
} double dis(int i,int j)
{
double distance;
double a = pow(station[i].x-station[j].x , );
double b = pow(station[i].y-station[j].y , );
double c = pow(station[i].z-station[j].z , );
distance = sqrt(a+b+c);
distance -= station[i].r + station[j].r;
if( distance <= ) distance = ;
return distance;
} void init()
{
for(int i= ; i<n ; i++)
scanf("%lf %lf %lf %lf",&station[i].x,&station[i].y,&station[i].z,&station[i].r);
for(int i= ; i<n ; i++)
road[i] = i;
need = ;
int k = ;
for(int i= ; i<n ; i++)
{
for(int j= ; j<n ; j++)
{
if( j>i )
{
edge[k].x = i;
edge[k].y = j;
edge[k].l = dis(i,j);
k++;
}
}
}
sort(edge,edge+n*(n-)/,cmp);
} void kruskal()
{
int i=,j=;
while( i<n- )
{
if( judge(edge[j].x,edge[j].y) )
need += edge[j].l , i++;
j++;
}
} int main()
{
while(cin>>n,n)
{
init();
kruskal();
printf("%.3f\n",need );
}
return ;
}
POJ2031 Building a Space Station【最小生成树】的更多相关文章
- POJ2031 Building a Space Station 2017-04-13 11:38 48人阅读 评论(0) 收藏
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8572 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 (最小生成树)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5173 Accepte ...
- POJ-2031 Building a Space Station (球的最小生成树)
http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...
- POJ - 2031C - Building a Space Station最小生成树
You are a member of the space station engineering team, and are assigned a task in the construction ...
- POJ Building a Space Station 最小生成树
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15664 Accepted: 6865 Description You ...
- poj2031 Building a Space Station
这题目,用G++ WA,用C++ AC. 题目要求,现给出n个球,然后要使每两个球直接或者间接连通,可以在任意两球之间做管道(在表面),最后的要求是,如果使得都连通的话,管道最小长度是多少. 思路简单 ...
- POJ 2031 Building a Space Station 最小生成树模板
题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...
- POJ 2031 Building a Space Station (最小生成树)
Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...
随机推荐
- BZOJ1001[BeiJing2006]狼抓兔子——最小割
题目描述 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: ...
- Code First 重复外键(简单方法)
之前有说过 Code First 重复外键 的一种解决方案. http://blog.csdn.net/hanjun0612/article/details/50478134 虽然可以解决问 ...
- poj1753 【枚举】
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- day29 类中的内置函数方法 __str__ __repr__ __call__ isinstance() issubclass()
__str__()__repr__()__len__() str() 转字符串repr() 让字符原形毕露的方法len() 计算长度 内置的方法很多,但是并不是全部都在object中,比如len(), ...
- android post 方式 访问网络 实例
android post 方式 访问网络 实例 因为Android4.0之后对使用网络有特殊要求,已经无法再在主线程中访问网络了,必须使用多线程访问的模式 该实例需要在android配置文件中添加 网 ...
- 【BZOJ2246】[SDOI2011]迷宫探险(搜索,动态规划)
[BZOJ2246][SDOI2011]迷宫探险(搜索,动态规划) 题面 BZOJ 洛谷 题解 乍一看似乎是可以求出每个东西是陷阱的概率,然而会发现前面走过的陷阱是不是陷阱实际上是会对当前状态产生影响 ...
- Shell基础知识(六)
shell中有很多内建命令,如何区分内建命令与外部文件,使用type command即可看到命令类型. >> type cd # input << cd is a Shell ...
- python开启httpserver服务在自动化测试中的一个小运用
httpserver可以在本机启动一个python实现的web服务器,在自动化测试中,可以将生成测试报告的目录开放给项目组同事. 先安装python 自动化测试框架,生成报告的目录D:\Automat ...
- [转载]C++的顺序点(sequence point)和副作用(side effect)
有关i=(++i)+(i++)这种东西的深入解释,不仅仅是简单粗暴undefined behavior. ==== 一.副作用(side effect) 表达式有两种功能:每个表达式都产生一个值( v ...
- CPP--借助神器VS理解内存存储(含大小端对齐)
单位,补码之类的可以看这个:http://www.cnblogs.com/dotnetcrazy/p/8178175.html 先说说大小端对齐的事情,然后再看: 内存最小单位==>Byte,i ...