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

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = ;
struct abc{ int start, end; double path; }node[maxn];
bool cmp(const abc&a, const abc&b){ return a.path < b.path; }
double x[maxn], y[maxn], z[maxn], r[maxn];
int father[maxn];
int find(int x)
{
if (x != father[x]) father[x] = find(father[x]);
return father[x];
}
int main()
{
int n, i, j;
while (~scanf("%d", &n))
{
if (n == ) break;
int tot = ;
for (i = ; i <= n; i++) scanf("%lf%lf%lf%lf", &x[i], &y[i], &z[i], &r[i]);
for (i = ; i <= n; i++) father[i] = i;
for (i = ; i <= n; i++)
{
for (j = i + ; j <= n; j++)
{
node[tot].start = i;
node[tot].end = j;
double tt = sqrt((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]) + (z[i] - z[j])*(z[i] - z[j]));
if (tt <= r[i] + r[j]) node[tot].path = ;
else node[tot].path = tt - r[i] - r[j];
tot++;
}
}
double ans = ;
sort(node, node + tot, cmp);
for (i = ; i < tot; i++)
{
int xx = find(node[i].start);
int yy = find(node[i].end);
if (xx != yy)
{
father[xx] = yy;
ans = ans + node[i].path;
}
}
printf("%.3lf\n", ans);
}
return ;
}

zoj 1718 poj 2031 Building a Space Station的更多相关文章

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

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

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

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

  3. POJ 2031 Building a Space Station

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

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

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

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

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

  6. POJ - 2031 Building a Space Station 三维球点生成树Kruskal

    Building a Space Station You are a member of the space station engineering team, and are assigned a ...

  7. POJ 2031 Building a Space Station (计算几何+最小生成树)

    题目: Description You are a member of the space station engineering team, and are assigned a task in t ...

  8. POJ 2031 Building a Space Station【最小生成树+简单计算几何】

    You are a member of the space station engineering team, and are assigned a task in the construction ...

  9. POJ 2031 Building a Space Station (prim裸题)

    Description You are a member of the space station engineering team, and are assigned a task in the c ...

随机推荐

  1. 【转】微信小程序专项测试

    微信小程序专项测试 by 云层 原文地址: http://mp.weixin.qq.com/s?__biz=MzA4NDIzNTIzNA==&mid=2654370226&idx=1& ...

  2. 【51Nod】1005 大数加法

    给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B Input示例 ...

  3. shell -- yes or no

    read -p "Do you want to .... [Y/N]? " yn while true; do case $yn in [Yy]|[Yy][Ee][Ss] ) br ...

  4. windows 下安装Python

    一.下载 官网地址:https://www.python.org/,64位的版本 二.安装 32位和64位的版本安装起来没有区别,双击打开后,第一步要记得勾上add python to Path 选项 ...

  5. 解决后端动态生成css时无法调用

    在PHP 设置头 header("Content-type: text/css");

  6. java知识点梳理

    网络搜索结果,出处不详,仅供参考 对于刚刚接触Java的人,java基础知识技术点繁多,那么gkstk小编为大家汇总最全java知识点如下,仅供大家参考学习! 1. JVM相关(包括了各个版本的特性) ...

  7. Python将列表中的string元素进行类型转换

    例如 将 a=['1','2.0','3L'] 转换为 a=[1,2.0,3L] 只需 map(eval,['1','2.0','3L']) 即可 eval(expression[, globals[ ...

  8. 使用FormData,进行Ajax请求并上传文件

    前段时间做了个手机端的图片上传,为了用户体验,用ajax交互,发现了FromData对象,这里有详细解释https://developer.mozilla.org/zh-CN/docs/Web/API ...

  9. Unity性能优化——LOD技术

    LOD,中文名多层次细节,是游戏中最常用的技术,它按照模型的位置和重要程度决定物体渲染的资源分配,降低非重要物体的面数和细节度,从而获得高效率的渲染运算.今天我们来实现使用它来做一个简单的优化例子. ...

  10. 建立tcl文件