这题目,用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. C++模板元编程 - 2 模仿haskell的列表以及相关操作

    这是昨天和今天写的东西,利用C++的可变模板参数包以及包展开,模式匹配的一些东西做的,感觉用typename...比轮子叔那个List<A,List<B, List<C, D> ...

  2. webview--网络超时

    package com.test.js2java; import java.util.Timer; import java.util.TimerTask; import android.app.Act ...

  3. Redis优化经验

    内存管理优化 Redis Hash是value内部为一个HashMap,如果该Map的成员数比较少,则会采用类似一维线性的紧凑格式来存储该Map, 即省去了大量指针的内存开销,这个参数控制对应在red ...

  4. DIV+CSS滑动门效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Hibernate 实体关联关系映射【转】

    Hibernate关联关系映射目录│ ├─单向关联│  ├─  一对一外键单向关联│  ├─  一对一主键单向关联│  ├─  一对一连接表单向关联│  ├─  一对多外键单向关联│  ├─  一对多 ...

  6. java类包第十一章

    1.同一个包中的类互相访问,不需要制定包名. 2.java中包名的规则是全部使用小写字母 3.final 方法不能被覆盖, public class OuterClass {    innerClas ...

  7. Python的方法分类

    1.Python的类方法,实例方法,和静态方法 class S(object): def Test(self): print("TEST") @classmethod#类方法 de ...

  8. Regional Changchun Online--Ponds

    网址:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)    Me ...

  9. Ansible状态管理

     转载自:http://xdays.me/ansible状态管理.html 简介 就像所有服务器批量管理工具(puppet有DSL,salt有state)一样,ansible也有自己的状态管理组件 ...

  10. (转)C# 解析 json

      原文: http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-json.html JSON(全称为JavaScript Object N ...