最小生成树,用了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. 爱上PowerShell

    Shell带来的好处是毋庸置疑的,当然也需要大量的时间去练习.PowerShell作为后起之秀, 同时试图打造一款更加人性化,更加易用的Shell.随着PowerShell开源跨平台的战略以及在Win ...

  2. .NET并行与多线程学习系列一

    并行与多线程学习系列一 一.并行初试: public static void test() { ; i < ; i++) { Console.WriteLine(i); } } public s ...

  3. UIView类绘图出现错误提示

    一:问题: Jan 16 15:49:53  CUBOT Band Ⅲ[2082] <Error>: CGContextSetLineWidth: invalid context 0x0. ...

  4. ES 6 : 函数的扩展

    1. 函数参数的默认值 [ 基本用法 ] 在ES6之前,不能直接为函数的参数指定默认值,只能采用变通的方法. 上面的代码检查函数log的参数y有没有赋值,如果没有,则指定默认值为world.这种写法的 ...

  5. Object-Widgets-Quick 构造树

    Object Tree 当以某个QObject为父类创建一个QObject时, 它会被添加到该父类的children列表中. 析构时, QObjet 会首先检查自己的children, 依次析构, 然 ...

  6. ps遇到的问题及笔记

    1. ps软件图层面板中不显示缩略图 打开软件后,任意打开一个文件,再打开图层面板(直接按F7也可),点图层面板右上角的小三角后有一个新的面板出现,点下面的"面板选项"弹出一个面板 ...

  7. Maven入门指南 :Maven 快速入门及简单使用

    开发环境 MyEclipse 2014 JDK 1.8 Maven 3.2.1 1.什么是Maven? Maven是一个Java语言编写的开源项目管理工具,是Apache软件基金会的顶级项目.主要用于 ...

  8. 浙大pat1050题解

    1050. String Subtraction (20) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard Given two string ...

  9. C# 语言规范_版本5.0 (第15章 委托)

    1. 委托 **(注:此章非常重要,特别是对于图形界面相关的区别于MFC和QT等的消息机制,委托是基石.) 委托是用来处理其他语言(如 C++.Pascal 和 Modula)需用函数指针来处理的情况 ...

  10. 洛谷-求同构数的个数-NOIP2013提高组复赛

    题目描述 Description 所谓同构数是指这样的数,即它出现在它的平方数的右端.例如,5的平方是25 (即5×5=25),5是25右端的数,那么5就是同构数.又如,25的平方是625(即25×2 ...