poj2031 Building a Space Station
这题目,用G++ WA,用C++ AC。
题目要求,现给出n个球,然后要使每两个球直接或者间接连通,可以在任意两球之间做管道(在表面),最后的要求是,如果使得都连通的话,管道最小长度是多少。
思路简单,就是求出来每两个球之间的距离,球心差减去两球的半径,注意如果这里是负的则处理为0。然后就是克鲁斯卡尔求最小生成树就可以了。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#define eps 0.00000001
struct point{
double x,y,z,r;
}p[];
struct edge{
int u,v;
double w;
}e[*];
int parent[];
int n,cnt;
void ufset(){
int i;
for(i=;i<=n;++i) parent[i]=-;
return;
}
int find(int x){
int s;
for(s=x;parent[s]>=;s=parent[s]);
while(s!=x){
int tmp=parent[x];
parent[x]=s;
x=tmp;
}
return s;
}
void Union(int R1,int R2){
int r1=find(R1),r2=find(R2);
int tmp=parent[r1]+parent[r2];
if(parent[r1]>parent[r2]){
parent[r1]=r2; parent[r2]=tmp;
}else{
parent[r2]=r1; parent[r1]=tmp;
}
return;
}
int cmp(struct edge a,struct edge b){
return eps<b.w-a.w;
}
double kruskal(){
double sumweight=;
int i;
int num=;
int u,v;
ufset();
for(i=;i<cnt;++i){
u=e[i].u; v=e[i].v;
if(find(u)!=find(v)){
sumweight+=e[i].w; num++;
// printf("sumweight=%lf,u=%d,v=%d\n",sumweight,u,v);
Union(u,v);
}
if(num>=n-) break;
}
return sumweight;
} double count(struct point a,struct point b){
double res;
res=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) ) - a.r - b.r ;
if( -res > eps ) res=;
return res;
} int main(){
int i,j;
while(~scanf("%d",&n)){
if(n==) break;
for(i=;i<n;++i){
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z,&p[i].r);
}
cnt=;
for(i=;i<n;++i){
for(j=;j<i;++j){
e[cnt].u=i;
e[cnt].v=j;
e[cnt].w=count(p[i],p[j]);
cnt++;
}
}
std::sort(e,e+cnt,cmp);
// for(i=0;i<cnt;++i){
// printf("%lf\n",e[i].w);
// }
// printf("------------\n");
double res=kruskal();
printf("%.3lf\n",res);
}
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 (球的最小生成树)
http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...
- POJ2031 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 ...
- 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 ...
随机推荐
- JadClipse eclipse反编译插件
A.下载JadClipse,http://jadclipse.sourceforge.net/wiki/index.php/Main_Page#Download,注意选择与eclipse版本一致的版本 ...
- windows下Gulp入门详细教程 &&gulp安装失败的原因(红色)
以下教程亲自实践可行: 另外添加一个Gulp自动编译.压缩.更新.测试的教程链接:https://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97%A8 ...
- linux下shell脚本学习
在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.您可以通过使用shell使大量的任务自动化,shel ...
- 二叉树的JAVA实现-二叉树的增删改查CRUD
package org.lyk.interfaces; import java.util.List; public interface IBiTree<T extends Comparable& ...
- awk 两列相减
cat http.txt |awk -F ':' '{print($2-$3)}' 百度文库
- bat文件创建mysql数据库 数据库名为meter
-- 详见附件,已自测通过 //修改为mysql安装路径 C:\Program Files\MySQL\MySQL Server 5.5" createtestd ...
- 使用 Heka 导入自定义的nginx日志到Elasticsearch
重置Heka执行进度 heka的进度配置文件存在配置项 base_dir 设置的目录,只需要删除这个文件夹下面的内容,就可以完全重置heka的进度. base_dir 配置项默认是在下面目录: '/v ...
- 微信红包签名算法 C#代码实现
string stringA = "appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=100001 ...
- MVC 4 与WebForm 混合应用 WebApi 发布常见问题
1.所有应用的MVC相关程序集编译时要选择复制到本地,需要用到的程序如下图 2.IIS设置: 因为 IIS 7/8 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改.运 ...
- 光流算法:灰度恒常约束,LK算法,HS算法
关于如何提高计算精度和增加搜索范围,见:http://www.cnblogs.com/dzyBK/p/4982420.html