这题目,用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的更多相关文章

  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 (球的最小生成树)

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

  3. POJ2031 Building a Space Station【最小生成树】

    题意: 就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则算为已连通,无需再搭桥.求搭建通路的最小边长总和是多少. 思路: 先处理空间点之间的距离 ...

  4. POJ 2031 Building a Space Station

    3维空间中的最小生成树....好久没碰关于图的东西了.....              Building a Space Station Time Limit: 1000MS   Memory Li ...

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

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

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

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

  7. poj 2031 Building a Space Station【最小生成树prime】【模板题】

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

  8. POJ2032 Building a Space Station(Kruskal)(并查集)

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

  9. POJ 2031 Building a Space Station【经典最小生成树】

    链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

随机推荐

  1. [实变函数]4.2 Egrov 定理

    1 一致收敛很重要, 但可惜的是很多时候不一致收敛. 比如 $$\bex f_n(x)=x^n\to f(x)=\sedd{\ba{ll} 0,&x\in [0,1)\\ 1,&x=1 ...

  2. 查看iis错误日志时提示找不到 freb.xsl的解决方法

    http://stackoverflow.com/questions/786638/how-can-i-get-gzip-compression-in-iis7-working/787251 Look ...

  3. JAVA 处理程序异常,(try、catch、finally),(thorws)

    一.try语句: try{//这里写可能出现异常的程序} catch(Exception e){//这里写如果出现异常怎么处理的程序} 二.throws语句 语法:函数方法()  throws Exc ...

  4. minicom的安装及使用

    1.下载 sudo apt-get install minicom 2.运行 sudo minicom -s 3.修改端口 首先查看端口:ls /dev/tty...

  5. java异常处理机制throws

    throws可以在方法声明时抛出的异常.原则上throws声明的异常,一定要在该方法中抛出,否则没有意义. 相反的,若方法中我们主动通过throw抛出一个异常,应该在throws中声明该种类异常,通知 ...

  6. 在asp.net 中应用POST传递和接收XML文件以及参数.(转)

    使用POST方式可以向别的页面发送请求,并获取返回结果. 可以从一个页面发送POST到另一个页面,也可以在winform工程中使用HTTPRequest发送POST到一个页面.我们拿ASP.NET的a ...

  7. java的io读取

    package gys; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; imp ...

  8. Ubuntu 之 initramfs 报错解决之一

    问题出现: ubuntu 更新后,编辑文件提示权限不够,并提示更新错误,重启后进入 initramfs ,仔细看提示错误有: file system check of the root filesys ...

  9. 将tomcat设为开机自动启动

    参考网址:http://jingyan.baidu.com/article/a65957f4b12b8724e77f9b5a.html 方法一 1.Tomcat的使用之前必须得Jre,至于是怎么装的, ...

  10. js数据结构与算法存储结构

    数据结构(程序设计=数据结构+算法) 数据结构就是关系,没错,就是数据元素相互之间存在的一种或多种特定关系的集合. 传统上,我们把数据结构分为逻辑结构和物理结构. 逻辑结构:是指数据对象中数据元素之间 ...