这题目,用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. java学习日志(1):命令行and小程序

    1.dos命令行,常见的命令 dir:列出当前目录下的文件以及文件夹md:创建目录rd:删除目录(必须空)cd:进入指定目录cd.. :退回到上一级目录cd/:退回到根目录del:删除文件exit:退 ...

  2. dos查看端口

    C:\Documents and Settings\Administrator>netstat -an | findstr "22"  TCP    192.168.16.2 ...

  3. ubuntu更换源后报错:W: GPG error: (转载)

    From:http://www.njava.com/njava-626.html 更换163源后,更新源时出现错误. # apt-get update W: GPG error: http://ext ...

  4. SVN---脱离SVN控制

    创建一个记事本文件,然后吧这句话复制进去for /r . %%a in (.) do @if exist "%%a\.svn" rd /s /q "%%a\.svn&qu ...

  5. jsp自定义标签1

    1.编写一个实现tag接口的java类 package cn.itcast.web.tag; import java.io.IOException; import javax.servlet.http ...

  6. fatal error C1853: '*.pch' is not a precompiled header file created with this compile

    在菜单下选择Build--->点击Rebuild All,如果不起作用,你可以先把Debug或者Release中的文件删除,然后再试.

  7. C API 连接MYSQL

    #include <iostream> #include <mysql.h> #include <string> #include <assert.h> ...

  8. zkclient

    zkclient 快速指南 Maven依赖 最新的版本发布在Maven中央库. <dependency> <groupId>com.github.adyliu</grou ...

  9. shell local

    Shell函数定义的变量默认是global的,其作用域从"函数被调用时执行变量定义的地方"开始,到shell结束 http://blog.chinaunix.net/xmlrpc. ...

  10. sikuli实例

    代码: package selenium.sikuli; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; im ...