1 /*
 2   最小生成树 + 几何判断
 3   Kruskal      球心之间的距离 - 两个球的半径 < 0 则说明是覆盖的!此时的距离按照0计算
 4 */
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<cmath>
 9 #include<algorithm>
10 using namespace std;
11 int f[105];
12 struct ball{
13    double x, y, z, r;
14 };
15
16 struct connect{
17    double dist;
18    int a, b;
19 };
20
21 connect c[5005];
22
23 ball b[105];
24
25 bool cmp(connect a, connect b){
26    return a.dist < b.dist;
27 }
28 int n;
29
30 int getFather(int x){
31    return x==f[x] ? x : f[x]=getFather(f[x]);
32 }
33
34 int Union(int a, int b){
35     int fa=getFather(a), fb=getFather(b);
36     if(fa!=fb){
37         f[fa]=fb;
38         return 1;
39     }
40     return 0;
41 }
42
43 int main(){
44    int i, j;
45    while(scanf("%d", &n) && n){
46       for(i=1; i<=n; ++i)
47          scanf("%lf%lf%lf%lf", &b[i].x, &b[i].y, &b[i].z, &b[i].r);
48       int cnt=0;
49       for(i=1; i<n; ++i)
50          for(j=i+1; j<=n; ++j){
51              double d = sqrt((b[i].x-b[j].x)*(b[i].x-b[j].x) + (b[i].y-b[j].y)*(b[i].y-b[j].y) + (b[i].z-b[j].z)*(b[i].z-b[j].z))
52                         - (b[i].r + b[j].r);
53                c[cnt].dist= d<0 ? 0: d;
54                c[cnt].a=i;
55                c[cnt++].b=j;
56          }
57        sort(c, c+cnt, cmp);
58        double minSum=0.0;
59        for(i=1; i<=n; ++i)
60           f[i]=i;
61        for(i=0; i<cnt; ++i){
62           if(Union(c[i].a, c[i].b))
63              minSum+=c[i].dist;
64        }
65        printf("%.3lf\n", minSum);
66    }
67    return 0;
68 }

转载于:https://www.cnblogs.com/hujunzheng/p/3877502.html

poj 2031Building a Space Station(几何判断+Kruskal最小生成树)相关推荐

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

    题目链接:http://poj.org/problem?id=2031点击打开链接 Building a Space Station Time Limit: 1000MS   Memory Limit ...

  2. POJ_2031 Building a Space Station

    Building a Space Station 链接 POJ_2031 Building a Space Station Describe You are a member of the space ...

  3. I. Space Station(hash记忆化+dp)

    <文章>陆游 文章本天成,妙手偶得之. 粹然无疵瑕,岂复须人为. 君看古彝器,巧拙两无施. 汉最近先秦,固已殊淳漓. 胡部何为者,豪竹杂哀丝. 后夔不复作,千载谁与期? I. Space ...

  4. 数据结构---Kruskal最小生成树

    数据结构-Kruskal最小生成树 原理:参考趣学数据结构 代码: 快速排序: #pragma once #define elemType int typedef struct vER {elemTy ...

  5. POJ 2031 Building a Space Station

    题目链接:http://poj.org/problem?id=2031 题目意思是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则算为已连通,无需再 ...

  6. zoj 1718 poj 2031 Building a Space Station

    最小生成树,用了Kruskal算法.POJ上C++能过,G++不能过... 算出每两个圆心之间的距离,如果距离小于两半径之和,那么这两个圆心之间的距离直接等于0,否则等于距离-R[i]-R[j]. # ...

  7. POJ - 3347 Kadj Squares(思维+几何)

    题目链接:点击查看 题目大意:给出n个正方形的边长,每个正方形必须与x轴有交点,且交点必须小,还需要满足每个正方形与x的交点依次递增,在满足以上条件的前提下,将所有的正方形防止在二维平面上,现在问从x ...

  8. 【POJ - 2392】Space Elevator (dp,优秀的背包问题)

    题干: The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a ...

  9. 【POJ - 1696】Space Ant (凸包,最小极角,排序)

    题干: The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists t ...

最新文章

  1. debian7更换gcc版本的二种方法分享
  2. HTTP代理如何正确处理Cookie(1)
  3. php7 echo无法显示,PHP7 echo和print语句实例用法
  4. CF1137C:Museums Tour(缩点、分层图)
  5. 动态规划训练16 [Doing Homework HDU - 1074 ]
  6. Javascript: IE中命名函数直接量的Bug?
  7. 设计模式之strategy模式(C++实现)
  8. 圣水观音湖规划--------------三维虚拟展示系统
  9. AVL树【图示详解+代码实现】
  10. 基于立体视觉的三维模型重建系统设计
  11. 企业wms系统安装在云服务器,wms云服务器配置
  12. WINRAR的破解方法
  13. Base64编码的原理及实现(源码)
  14. android平台下OpenGL ES 3.0绘制圆点、直线和三角形
  15. Linux命令教程第二期
  16. 致读者:冰河技术微信公众号的重大调整!!
  17. React-Native之定位实践
  18. 成绩预警系统c语言,学分预警系统项目描述
  19. 局域网内环境搭建-PC篇
  20. CSS——响应式布局案例以及点击出现下拉框实现过程

热门文章

  1. php 商品展示html,HTML5和CSS3实现3D展示商品信息的代码
  2. mysql 通过存储过程 插入测试百万数据
  3. Sublime Text 3 快捷键总结(详细版本)
  4. 将本地源代码程序推送远程Github仓库
  5. AOP+自定义注解 实现service统一的异常信息处理
  6. aspx转发php_asp,php,aspx一句话合集
  7. datagridview选中获取行号_DataGridView控件显示行号的正确代码及分析
  8. input css年月日,input标签的type为date,显示的日期格式样式更改
  9. 回旋滚动_中频炉电动旋转轴承,管道回旋轴承,电炉旋转轴承
  10. 头部外伤指什么_什么是颅骨缺损?