题意:

就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通。如果两个球有重叠的部分则算为已连通,无需再搭桥。求搭建通路的最小边长总和是多少。

思路:

先处理空间点之间的距离,要注意的是两个球面相交的情况,相交的话距离是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【最小生成树】的更多相关文章

  1. 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 ...

  2. POJ 2031:Building a Space Station 最小生成树

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6083   Accepte ...

  3. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5173   Accepte ...

  4. POJ-2031 Building a Space Station (球的最小生成树)

    http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...

  5. POJ - 2031C - Building a Space Station最小生成树

    You are a member of the space station engineering team, and are assigned a task in the construction ...

  6. POJ Building a Space Station 最小生成树

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15664   Accepted: 6865 Description You ...

  7. poj2031 Building a Space Station

    这题目,用G++ WA,用C++ AC. 题目要求,现给出n个球,然后要使每两个球直接或者间接连通,可以在任意两球之间做管道(在表面),最后的要求是,如果使得都连通的话,管道最小长度是多少. 思路简单 ...

  8. POJ 2031 Building a Space Station 最小生成树模板

    题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...

  9. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...

随机推荐

  1. StringBuilder String string.Concat 字符串拼接速度再议

    首先看测试代码: public class StringSpeedTest { "; public string StringAdd(int count) { string str = st ...

  2. UVALive5874 - Social Holidaying-二分图匹配/匈牙利算法

    有n个家庭,m个房间,一个房间只能两个家庭住.求最大匹配. 比较标准的二分图问题.先初始化把可能的家庭建边,然后跑一边匈牙利算法. 最后的答案是最大匹配数/2,因为建图时有重复. #include & ...

  3. Codeforces Round #337 (Div. 2) B. Vika and Squares

    B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. mysql中存储过程

    存储过程procedure 存储过程,其本质还是函数——但其规定:不能有返回值: 定义形式: 说明: 1,in:用于设定该变量是用来“接收实参数据”的,即“传入”:默认不写,就是in 2,out:用于 ...

  5. Android 判定手机是否root

    Android获取手机root的状态 package com.app.demo; import java.io.File; import android.app.Activity; import an ...

  6. 【bzoj1797】 Ahoi2009—Mincut 最小割

    http://www.lydsy.com/JudgeOnline/problem.php?id=1797 (题目链接) 题意 求一条边是否可能在一个最小割集中,以及这条边是否一定在最小割集中. Sol ...

  7. 01---JMS与消息中间件的基本概念

    JMS消息服务介绍和使用场景 什么是JMS JMS : Java Message Service(Java消息服务),Java平台中关于面向消息中间件的接口. 重点在于接口,接口就意味着与JDBC类似 ...

  8. 【codevs4829】数字三角形++

    题目大意:给定一个数字三角形,求从 (1,1) 到第 N 行的路径经过的权值之和加上该路径上任意一个点的权值之和的最大值. 题解:任意加一条路径上的某个值,可以看成是多了一次选择的权利,即:在每次经过 ...

  9. Java: String.split(....); 结果很意外

    String txt = "join|公共聊天室||"; String[] paras = txt.splite("\\|"); String t1 = par ...

  10. FTP文件乱码和传输模式解释

    转: FTP文件乱码和传输模式解释 2017年02月18日 10:50:03 -Hermes- 阅读数:12112更多 所属专栏: 异常解决方案急诊室   版权声明:大侠,在转载时请注明出处,小弟不胜 ...