题目链接: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. CouchDB 未授权访问漏洞

    0x00 CouchDB安装 CouchDB官方网站:http://couchdb.apache.org/ 一路Next直到完成安装,打开浏览器并访问以下链接:http://127.0.0.1:598 ...

  2. Ruby Tutorial

    http://www.tutorialspoint.com/ruby/ruby_quick_guide.htm http://www.cnblogs.com/PurpleCow/archive/201 ...

  3. SpringMVC系列之URL匹配问题

    一.工程目录 二.web.xml配置文件及与其他文件的关系 三.控制器部分 四.返回值 五.url前后缀 六.项目源代码 http://files.cnblogs.com/files/xujian20 ...

  4. Linux下Apache网站目录读写权限的设置

    网站目录文件权限的设置对网站的安全至关重要,下面简单介绍网站目录文件权限的基本设定. 我们假设http服务器运行的用户和用户组是www,网站用户为centos,网站根目录是/home/centos/w ...

  5. Use Reentrant Functions for Safer Signal Handling(译:使用可重入函数进行更安全的信号处理)

    Use Reentrant Functions for Safer Signal Handling 使用可重入函数进行更安全的信号处理 How and when to employ reentranc ...

  6. java基础---->使用Itext生成数据库文档

    这里简单的介绍一下使用Itext生成数据库表的文档.于是我们领教了世界是何等凶顽,同时又得知世界也可以变得温存和美好. 生成数据库的文档 一.maven项目需要引入的jar依赖 <depende ...

  7. 【Java基础系列】Java IO系统

    前言 创建好的输入/输出系统不仅要考虑三种不同种类的IO系统(文件,控制台,网络连接)还需要通过大量不同的方式与他们通信(顺序,随机访问,二进制,字符,按行,按字等等). 一.输入和输出 Java的I ...

  8. 【大数据系列】hadoop集群设置官方文档翻译

    Hadoop Cluster Setup Purpose Prerequisites Installation Configuring Hadoop in Non-Secure Mode Config ...

  9. 【Spring】依赖注入 加载顺序

    一.Spring依赖注入depents-on参数 depents-on是指指定Bean初始化及销毁时的顺序,使用depends-on属性指定的是Bean要先初始化完毕后才初始化当前Bean,由于只有S ...

  10. LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)

    题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description   给出一个未排序的数组,求出第一个丢失的正数. ...