Building a Space Station(bfs)
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)的更多相关文章
- POJ 2031 Building a Space Station
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- POJ 2031 Building a Space Station (最小生成树)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5173 Accepte ...
- POJ 2031 Building a Space Station (最小生成树)
Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...
- poj 2031 Building a Space Station【最小生成树prime】【模板题】
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5699 Accepte ...
- 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 ...
- POJ2032 Building a Space Station(Kruskal)(并查集)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7469 Accepte ...
- POJ 2031 Building a Space Station【经典最小生成树】
链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- Building a Space Station POJ - 2031
Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...
- 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 ...
随机推荐
- Mock随机生成数据模拟后台接口
<html> <head> <title>测试</title> <script src="http://code.jquery.com/ ...
- 【转】下载对应内核版本的asmlib
登陆oracle官网: http://www.oracle.com/ 注意:查询前,需要注册一个OTN账号,注册是免费的. 首页直接搜索“ASMLib”关键词(注意大小写) 进入搜索界面,点击第一项即 ...
- 使用原生JS的AJAX读取json全过程
首先ajax(async javascript and xml)是用于前端与后端文件比如xml或者json之间的交互.他是一种异步加载技术,意味着你点击某个加载事件是再也不用刷新整个页面,而是发送局部 ...
- vue 打印 页面特定部分转pdf
https://www.jb51.net/article/147040.htm https://www.jianshu.com/p/dd120b65446a //转pdf
- 通过JS唤醒app(安卓+ios)
有需求说要通过页面按钮唤醒app,或者手机上没有这款app跳转到商店,然后刚开始也是查了资料的,结果发现一头雾水,不过最后还是捣鼓出来了,当然也参考了前人分享的经验,下面我就将方法整理一下: 首先明确 ...
- 使用python的几个小经验(查看文档)
好久没有水博客了,未来再过20天不到的时间又得参加软考,今天终于得好好水一发帖子 关于Python,很多人包括我之前都不知道怎么找文档,现在有一个好办法,就是在命令行模式下调用pydoc –p xxx ...
- springboot 使用idea打包 遇到问题
找了很久错误后来发现添加这三句话就可以了 需要在项目的pom.xml文件中加上第47-49行的3句话
- 二叉堆 及 大根堆的python实现
Python 二叉堆(binary heap) 二叉堆是一种特殊的堆,二叉堆是完全二叉树或者是近似完全二叉树.二叉堆满足堆特性:父节点的键值总是保持固定的序关系于任何一个子节点的键值,且每个节点的左子 ...
- Linux - VMware和Centos安装
目录 Linux - VMware和Centos安装 选择性 下载centos系统ISO镜像 安装虚拟机VMware虚拟机 1. 准备vmware软件 2. 解压软件包, 当前选择vm12 3. vm ...
- 【6】Django视图函数
治大国若烹小鲜.以道莅天下 --老子<道德经> 本节内容 Django web项目的运行流程分析 视图处理函数的定义 多视图处理函数及接收参数 1. web项目运行流程分析 通常情况下,完 ...