最小生成树,用了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. 如何把微信语音汇总成一个MP3文件?

    有的时候想要保存微信中的语音内容,但是苦于语音短且多,因此想要把它汇总成一个音频文件. 本篇以苹果手机为例,安卓手机也可类似. 第一步,安装同步助手 同步助手是一款在电脑上安装,可以保存手机上的内容的 ...

  2. NTSC色域(CIE1931)计算公式

    色域(CIE1931)=ABS(RC[-6]*RC[-3]+RC[-4]*RC[-1]+RC[-2]*RC[-5]-RC[-6]*RC[-1]-RC[-4]*RC[-5]-RC[-2]*RC[-3]) ...

  3. js 增加 onclick 事件

    obj.onclick = function(){check(this)} function check(obj){ alert(obj)l }

  4. Ubuntu环境openresty的安装

    Ubuntu环境openresty的安装 相关库的安装 安装openresty需要的库  apt-get install libreadline-dev libncurses5-dev libpcre ...

  5. STM32驱动ht1621b显示LCD

    这几天在写ht1621b显示LCD的程序,主芯片是Stm32f10的芯片.对于stm32和ht1621b的运用和操作本人是新手,属于赶鸭子上架,通过查看datasheet等资料和网上查看前人写的程序终 ...

  6. Navicat连接不上MySQL

    [root@localhost init.d]# pwd /etc/init.d [root@localhost init.d]# mysql -u root -p Enter password: E ...

  7. Unity3D脚本使用:Random

    实例: 为集合变量赋值,并运行,点击按钮,运行结果如图   

  8. Linux 操作命令列表记录

    Linux 操作命令列表记录 SSH登录 登录 ## 范式 ssh [username]@[host] ## 例 ssh -p 1222 root@10.0.0.1 使用非默认端口(ssh默认端口22 ...

  9. HashMap按键排序和按值排序

    对map集合进行排序   今天做统计时需要对X轴的地区按照地区代码(areaCode)进行排序,由于在构建XMLData使用的map来进行数据统计的,所以在统计过程中就需要对map进行排序. 一.简单 ...

  10. C#泛型委托及约束

    泛型委托: namespace 泛型委托 { public delegate void Mydelegate<T>(T msg); class Program { static void ...