题目链接:http://poj.org/problem?id=2031

n个球型的cell,如果任意两个球表面没有接触或者没有包含关系,就选择最近的表面建立通道;

所以用maps[i][j]表示i,j之间的距离;最后求所有连接的和的最小值;

注意:

poj上:对于双精度输出,G++上面要用%f,C++则用%lf,否则WA

#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<math.h>
#define N 110
#define INF 0xfffffff using namespace std; int vis[N], n;
double maps[N][N], dist[N]; struct node
{
double x, y, z, r;
}a[N*N]; void Init()
{
int i;
memset(a, ,sizeof(a));
memset(vis, ,sizeof(vis));
for(i=; i<=n; i++)
{
dist[i] = INF;
for(int j=; j<=n; j++)
{
maps[i][j] = INF;
}
maps[i][i] = ;
}
} double Prim(int start)
{
double ans = ;
vis[start] = ;
for(int i=; i<=n; i++)
dist[i] = maps[start][i];
for(int i=; i<=n; i++)
{
double Min = INF;
int index = -;
for(int j=; j<=n; j++)
{
if(vis[j] == && Min > dist[j])
{
Min = dist[j];
index = j;
}
}
if(index == -)break;
vis[index] = ;
ans += Min;
for(int j=; j<=n; j++)
{
if(vis[j] == && dist[j] > maps[index][j])
dist[j] = maps[index][j];
}
}
return ans;
} int main()
{
int i, j;
double ans, x, y, z, d;
while(scanf("%d", &n), n)
{
Init();
for(i=; i<=n; i++)
{
scanf("%lf%lf%lf%lf", &a[i].x, &a[i].y, &a[i].z, &a[i].r);
}
for(i=; i<=n; i++)
{
for(j=i+; j<=n; j++)
{
x = a[i].x - a[j].x;
y = a[i].y - a[j].y;
z = a[i].z - a[j].z;
d = sqrt( x*x + y*y + z*z );
if(d > a[i].r + a[j].r)
{
maps[i][j] = maps[j][i] = d - a[i].r - a[j].r;
}
else
{
maps[i][j] = maps[j][i] = ;
}
}
}
ans = Prim();
printf("%.3lf\n", ans);
}
return ;
}

Building a Space Station---poj2031(最小生成树)的更多相关文章

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

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

  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 (计算几何+最小生成树)

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

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

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

  5. POJ2031 Building a Space Station【最小生成树】

    题意: 就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则算为已连通,无需再搭桥.求搭建通路的最小边长总和是多少. 思路: 先处理空间点之间的距离 ...

  6. poj 2031 Building a Space Station(最小生成树,三维,基础)

    只是坐标变成三维得了,而且要减去两边的半径而已 题目 //最小生成树,只是变成三维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> ...

  7. POJ2031 Building a Space Station 2017-04-13 11:38 48人阅读 评论(0) 收藏

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

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

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

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

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

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

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

随机推荐

  1. scala中Map和Tuple

    /** * Created by root * Description : Tuple and Map */ object MapTest { def main(args: Array[String] ...

  2. 【RF库XML测试】通过xpath查找元素的说明

    Tag names:当仅使用1个tag的时候,xpath匹配具有该标签名称的所有直接子元素. Paths:通过/符号连接tag名称,例如second/child将匹配父元素second下所有的chil ...

  3. mysql官方下载

    https://dev.mysql.com/downloads/file/?id=472899

  4. PHP域名解析(一个IP绑多域名)----看看可以,并不值得借鉴

    PHP域名解析(一个IP绑多域名)----看看可以,并不值得借鉴 好处当然是不用买多网卡.不用设置其它端口为WEB端口了,一张网卡上.都用同一个80端口建很多网站. 假设有三个域名:     [url ...

  5. Git学习(一)(2015年11月12日)

    环境:win10 已安装git工具(如未配置环境变量需先配置环境变量) 如何配置环境变量:.我的电脑-属性-高级系统设置-环境变量-系统变量 找到path然后在变量值结尾增加路径: ;C:\Progr ...

  6. css 去除页面双击事件

    -ms-touch-action: manipulation !important;

  7. JS 运行、复制、另存为 代码。

    //运行代码 function runEx(cod1) { cod = document.getElementById(cod1) var code = cod.value; if (code != ...

  8. 日记整理---->2016-11-21

    2016-11-21简单的总结一下学到的知识点.作为一个目标而存在的东西,总是那么美丽而优雅. 一.PE中事务的编写 getTransactionTemplate().execute(new Tran ...

  9. 【大数据系列】MapReduce详解

    MapReduce是hadoop中的一个计算框架,用来处理大数据.所谓大数据处理,即以价值为导向,对大数据加工,挖掘和优化等各种处理. MapReduce擅长处理大数据,这是由MapReduce的设计 ...

  10. sencha touch 在线实战培训 第一期 第六节

    2014.1.11晚上8点开的课 本来计划8号晚上开课的,不过那天晚上小区电路出了问题,所以没有讲成.后面两天我又有点其他的事情,所以放到了11号来讲. 本期培训一共八节,前三堂免费,后面的课程需要付 ...