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 ...
随机推荐
- servlet生成验证码代码
package forward; import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt ...
- BZOJ2215[Poi2011]Conspiracy——2-SAT+tarjan缩点
题目描述 Byteotia的领土被占领了,国王Byteasar正在打算组织秘密抵抗运动.国王需要选一些人来进行这场运动,而这些人被分为两部分:一部分成为同谋者活动在被占领区域,另一部分是后勤组织在未被 ...
- BZOJ1926[Sdoi2010]粟粟的书架——二分答案+主席树
题目描述 幸福幼儿园 B29 班的粟粟是一个聪明机灵.乖巧可爱的小朋友,她的爱好是画画和读书,尤其喜欢 Thomas H. Co rmen 的文章.粟粟家中有一个 R行C 列的巨型书架,书架的每一个位 ...
- Educational Codeforces Round 58 Div. 2 自闭记
明明多个几秒就能场上AK了.自闭. A:签到. #include<iostream> #include<cstdio> #include<cmath> #inclu ...
- HDU4623 CRIME 【状压DP】【同类项合并】
题目大意: 求相邻元素互质的排列个数. 题目分析: 由于互质只与质因数有关,所以我们对于质因数种类相同的数合并为一类,特殊的,1,17,19,23是一类,因为没有数与他们不互质. 那么我们做各个位进制 ...
- MT【26】ln(1+x)的对数平均放缩
评:1.某种程度上$ln(1+x)\ge \frac{2x}{2+x}$是最佳放缩. 2.这里涉及到分母为幂函数型的放缩技巧,但是不够强,做不了这题.
- android 图片处理经验分享
在设置ImageView资源的时候,这时的图片是来自SD卡,查看API很容易就会看到view.setImageUri(Uri u)这个函数.所以一般会这样写: ImageView view = (I ...
- 使用sharepoint里Open with explorer功能
使用这个功能时,遇到几个问题: 1. 当点击library时,ie报错:A problem with this webpage caused Internet Explorer to close an ...
- luogu4269 Snow Boots G (并查集)
对于某个靴子,如果0代表某个格能走,1代表不能走,那么只要连续的1的个数的最大值>=靴子的步长,那这个靴子就不能用. 那么只要对靴子和格子都按深度排个序,然后从大到小来扫一遍(靴子越来越浅,能走 ...
- numpy random
np.random.seed()用法: np.random.seed(5) print(np.random.permutation(np.array([i for i in range(9)]))) ...