Building a Space Station
poj2031:http://poj.org/problem?id=2031
题意:就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通。如果两个球有重叠的部分则算为已连通,无需再搭桥。求搭建通路的最小费用(费用就是边权,就是两个球面之间的距离)。
题解:其实就是图论的最小生成树问题球心坐标和半径是用来求 两点之间的边权 的,求出边权后,把球看做点,用邻接矩阵存储这个无向图,再求最小生成树,非常简单的水题把球A和球B看做无向图图的两个结点,那么边权 = AB球面距离 = A球心到B球心的距离 – A球半径 – B球半径 .注意若边权<=0,说明两球接触,即已连通,此时边权为0
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,pa[],num,cnt;
struct Edge{
int u;
int v;
double w;
bool operator <(const Edge &a)const{
return w<a.w;
}
}edge[];
struct Node{
double x;
double y;
double z;
double r;
}node[];
void UFset(){
for(int i=;i<=n;i++)
pa[i]=-;
}
int Find(int x){
int s;
for(s=x;pa[s]>=;s=pa[s]);
while(s!=x){
int temp=pa[x];
pa[x]=s;
x=pa[x];
}
return s ;
}
void Union(int R1,int R2){
int r1=Find(R1);
int r2=Find(R2);
int temp=pa[r1]+pa[r2];
if(pa[r1]>pa[r2]){
pa[r1]=r2;
pa[r2]=temp;
}
else{
pa[r2]=r1;
pa[r1]=temp;
}
}
void kruska(){
UFset();
num=;
double sum=0.0;
for(int i=;i<cnt;i++){
int u=edge[i].u;
int v=edge[i].v;
if(Find(u)!=Find(v)){
sum+=edge[i].w;
Union(u,v);
num++;
}
if(num>=n-)break;
}
printf("%.3f\n",sum);
}
int main(){
while(~scanf("%d",&n)&&n){
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&node[i].x,&node[i].y,&node[i].z,&node[i].r);
}
cnt=;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++){
double s1=node[i].x-node[j].x;
double s2=node[i].y-node[j].y;
double s3=node[i].z-node[j].z;
double ss=sqrt(s1*s1+s2*s2+s3*s3);
if(ss>node[i].r+node[j].r){
edge[cnt].u=i;edge[cnt].v=j;edge[cnt++].w=ss-node[i].r-node[j].r;
}
else{
edge[cnt].u=i;edge[cnt].v=j;edge[cnt++].w=;
}
} sort(edge+,edge+cnt);
kruska();
}
}
Building a Space Station的更多相关文章
- POJ 2031 Building a Space Station
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- 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 (最小生成树)
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 ...
- 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 ...
- POJ2032 Building a Space Station(Kruskal)(并查集)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7469 Accepte ...
- POJ 2031 Building a Space Station【经典最小生成树】
链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- Building a Space Station POJ - 2031
Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...
- 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 2931 Building a Space Station <克鲁斯卡尔>
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5869 Accepted: 2 ...
随机推荐
- 理解 Linux 网络栈(1):Linux 网络协议栈简单总结 图
http://www.cnblogs.com/sammyliu/p/5225623.html
- Visual Studio 调试技巧 (二)-- 为中断设置条件
今天尽是干货.我们来讨论如何为中断设置条件吧. 就像习大大讲的精确扶贫一样,如果我们能很精确地,仅在需要的时候把断点命中,以查看这个时候的程序数据,我们就能显著地提高 Debug 的效率.为断点设置条 ...
- J2EE 读取文件路径
在J2ee中实现java类读取webcontent/web-inf/config.xml的实现代码 ,其中../config.xml相对于classes的路径 java.net.URL url = t ...
- VNC服务端自动化配置脚本
在使用阿里云的linux云主机,看到官方提供的远程连接服务器bash脚本,记录下来. 功能:自动修改系统源和安装vncserver相关的软件包,centos.redhat系列都是安装gno ...
- codevs 2149 矩形周长(暴力扫描线)
/* 暴力应该很好理解 不多说了 至于线段树维护的嘛 还没看懂 哪天突然想明白了在写吧 */ #include<iostream> #include<cstdio> #incl ...
- [转]Web UI 设计命名规范
来源:http://blog.bingo929.com/web-ui-design-name-convention.html 一.网站设计及基本框架结构: 1. Container “conta ...
- Netbeans IDE配置
- style currentStyle getComputedStyle的区别和用法
先介绍下层叠样式表的三种形式: 1.内联样式,在html标签中style属性设置. <p style="color:#f90">内联样式</p> 2.嵌入样 ...
- Servlet(一)
BS架构的优势 1.数据库之负责数据库的管理 2.Web服务器负责业务逻辑的处理 3.浏览器提供操作界面 4.不需要单独安装客户端 5.开发相对于CS简单,客户端和服务器的通信模块都是使用标准的HTT ...
- 【ZOJ2112】【整体二分+树状数组】带修改区间第k大
The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...