空间站是有一些球状的房间组成的,现在有一些房间但是没有相互连接,你需要设计一些走廊使他们都相通,当然,有些房间可能会有重合(很神奇的样子,重合距离是0),你需要设计出来最短的走廊使所有的点都连接。

分析:因为给的都是点的坐标,所以构图的时候会有一些麻烦,不过也仅此而已。。。
*******************************************************************
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
using namespace std; #define maxn 105 struct point{double x, y, z, r;}p[maxn];
struct node
{
    int u, v;
    double len;     friend bool operator < (node a, node b)
    {
        return a.len > b.len;
    }
}; int f[maxn]; double Len(point a, point b)//求两点间的距离
{
    double x = a.x - b.x;
    double y = a.y - b.y;
    double z = a.z - b.z;
    double l = sqrt(x*x+y*y+z*z)-a.r-b.r;     if(l < 0)l = 0;     return l;
}
int Find(int x)
{
    if(f[x] != x)
        f[x] = Find(f[x]);
    return f[x];
} int main()
{
    int N;     while(scanf("%d", &N) != EOF && N)
    {
        int i, j;
        node s;
        priority_queue<node>Q;         for(i=1; i<=N; i++)
        {
            scanf("%lf%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z, &p[i].r);
            f[i] = i;
        }         for(i=1; i<=N; i++)
        for(j=i+1; j<=N; j++)
        {
            s.u = i, s.v = j;
            s.len = Len(p[i], p[j]);
            Q.push(s);
        }         double ans = 0;         while(Q.size())
        {
            s = Q.top();Q.pop();
            int u = Find(s.u), v = Find(s.v);             if(u != v)
            {
                f[u] = v;
                ans += s.len;
            }
        }         printf("%.3f\n", ans);
    }     return 0;
}

C - Building a Space Station - poj 2031的更多相关文章

  1. Building a Space Station POJ - 2031

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

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

    链接: http://poj.org/problem?id=2031 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6011 ...

  3. Building a Space Station POJ 2031 【最小生成树 prim】

    http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...

  4. Building a Space Station POJ - 2031 三维最小生成树,其实就是板子题

    #include<iostream> #include<cmath> #include<algorithm> #include<cstdio> usin ...

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

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

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

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

  7. POJ 2031 Building a Space Station

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

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

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

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

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

随机推荐

  1. centos 6+安装山逗斯骚尅特(本文内容来自都比更具帝)

    系统支持:CentOS 6+,Debian 7+,Ubuntu 12+ 内存要求:≥128M 关于本脚本 一键安装 Shadowsocks-Python, ShadowsocksR, Shadowso ...

  2. 9.13noip模拟试题

    题目名称 “与” 小象涂色 行动!行动! 输入文件 and.in elephant.in move.in 输出文件 and.out elephant.in move.in 时间限制 1s 1s 1s ...

  3. 9.8 noip模拟试题

    LazyChild黑OJ(blackoj.pas/c/cpp) LazyChild开了一家“善良OJ”.但大多数人都不知道,这其实是家黑OJ.亲爱的同学,请不要惊讶,古时候有黑店,现代为什么不能有黑O ...

  4. codevs2622数字序列( 连续子序列最大和O(n)算法)

    /* 算法描述:维护一个s[p]表示累加和 并且更新最大值ans 如果s[p]<0 则从p+1重新累加 证明:设某个区间的起点和终点分别为s t 分两种情况 1.t<p:设s2表示1到s的 ...

  5. [转]delphi 删除动态数组的指定元素

    type TArr = array of TPoint; {把数组先定义成一个类型会方便许多, 这里仅用 TPoint 测试} {删除动态数组指定元素的过程: 参数 arr 是数组名, 参数 Inde ...

  6. 学习springMVC实例1——配置和跳转到HelloWorld

    本文让大家迅速掌握springMVC的使用方法,以最简单的方式理解此框架 一.用eclipse新建一个web项目,命名为springMVC1,tomcat的端口号为9090 二.在WEB-INF目录下 ...

  7. 你好,C++(9)坐216路公交车去买3.5元一斤的西红柿——C++中如何表达各种数值数据 3.3 数值数据类型

    3.3  数值数据类型 从每天早上睁开眼睛的那一刻开始,我们几乎每时每刻都在与数字打交道:从闹钟上的6点30分,到上班坐的216路公共汽车:从新闻中说的房价跌到了100元每平米到回家买菜时的西红柿3. ...

  8. [500lines]500行代码写web server

    项目地址:https://github.com/aosabook/500lines/tree/master/web-server.作者是来自Mozilla的Greg Wilson.项目是用py2写成. ...

  9. [jQuery] $.grep使用

    1.$.grep的功能是查找过滤功能的数组,原数组不受影响. 2.参数定义 jQuery.grep( array, function(elementOfArray, indexInArray), [ ...

  10. Windows下命令行连接mysql及导入sql文件

    嗯,今天要把phpcms的模板放到服务器上,,,呃,phpMyAdmin死活连接不上数据库,这又是个神马情况无奈,又想到命令行了,好吧,最近喜欢上命令行了,不过这果然还是命令行强大啊,啊哈哈下面呢,我 ...