http://poj.org/problem?id=2031

题意:给出n个球的圆心坐标x,y,z, 半径r,若任意两球不相交,则在两球间建桥。问需建桥的最短距离是多少。

思路:建图,以两球间相差的距离为权值,然后求最小生成树。

 #include <stdio.h>
#include <math.h>
#include <string.h>
const int inf=<<;
const double eps=1e-;
const int maxn=;
double sum,Map[maxn][maxn],dis[maxn];
int vis[maxn];
struct point
{
double x,y,z,r;
};
void prim(int n)
{
for (int i = ; i <= n; i++)
dis[i]=inf;
dis[]=;
sum=;
for (int i = ; i <= n; i++)
{
double Min=inf;
int pos=;
for (int j = ; j <= n; j++)
{
if (!vis[j]&&dis[j] < Min)
{
Min = dis[j];
pos = j;
}
}
if (Min==inf)
return;
vis[pos] = ;
sum+=Min;
for (int j = ; j <= n; j++)
{
if (!vis[j]&&dis[j] > Map[pos][j])
dis[j] = Map[pos][j];
}
}
}
void init()
{
sum = ;
memset(vis,,sizeof(vis));
memset(Map,,sizeof(Map));
}
double dist(point &a,point &b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z);
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
point a[];
init();
for (int i = ; i <= n; i++)
{
scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z,&a[i].r);
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
double d = sqrt(dist(a[i],a[j]));
if (d-(a[i].r+a[j].r)>eps)
Map[i][j] = d-a[i].r-a[j].r;
else
Map[i][j] = ;
}
}
prim(n);
printf("%.3f\n",sum);
}
return ;
}

Building a Space Station(bfs)的更多相关文章

  1. POJ 2031 Building a Space Station

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

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

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

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

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

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

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

  5. 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 ...

  6. POJ2032 Building a Space Station(Kruskal)(并查集)

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

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

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

  8. Building a Space Station POJ - 2031

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

  9. 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 ...

随机推荐

  1. git生成ssh key及本地解决多个ssh key的问题

    git生成ssh key及本地解决多个ssh key的问题 ssh是一种网络协议,用于计算机之间的加密登录.ssh原理及应用可参考: SSH原理与运用(一):远程登录 生成ssh key步骤 这里以配 ...

  2. 使用码云gitee.com托管代码

    1.新建项目 可以看到团队资源管理器是这样的,已经在本地有存储库,所有更改可以保存本地 2.在码云上新建项目 项目名称必填,其它项根据情况填写 3.复制项目地址关联到本地存储库 填写码云的项目地址,发 ...

  3. 报错:command not found

    linux中如果是最小化安装的系统,执行命令的时候很多会出现没找到命令 [root@localhost ~]# mtr -bash: mtr: command not found [root@loca ...

  4. 最小生成树算法Kruskal详解

    要讲Kruskal,我们先来看下面一组样例. 4 5 1 2 3 1 4 5 2 4 7 2 3 6 3 4 8 14 画出来更直观一些,就是上面的这张图. 智商只要不是0的(了解最小生成树是什么的童 ...

  5. pip/pip3国内源

    Error 在使用pip3安装PySide2时出现ReadTimeoutError. $ pip3 install PySide2 Solution 使用国内源 例如: $ pip3 install ...

  6. 熟悉RHEL7登录界面使用

    Linux操作系统提供了图像界面和字符界面两种操作环境. 图像界面: 1.开启RHEL7后进入到该界面,图中用户是我们创建的本地用户,如果我们要以管理员身份登录则点击Not listed(未列出). ...

  7. mongodb数据库的导出与导入

    数据库的导出 导出类型为json,数据库:mapdb,集合:bike 字段:bikeId,lat,lng,current_time,source ,条件为source字段为ofo第一条数据 mongo ...

  8. python第十二周:MySql

    MySql数据库 MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司.MySQL 是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不 ...

  9. seminar information (Email template)

      The following is an email example of seminar information   **************** Dear all, It is a plea ...

  10. Huawei-R&S-网络工程师实验笔记20190608-VLAN划分基础(基于端口、MAC地址、子网地址、协议)

    >Huawei-R&S-网络工程师实验笔记20190608-VLAN划分基础(基于端口.MAC地址.子网地址.协议) >>实验开始,先上拓扑图参考: 一.基于端口划分VLAN ...