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 ...
随机推荐
- hibernate Hql 更新的参数只能设置String类型
最近在项目中发现一个奇怪的现象,请看下面的代码 实体类MyEmployeeEntity @Table(name="myemployee") public class MyEmplo ...
- FusionCharts 更新 chart data 数据
var AlarmChart = null; function alarmChartReBuild(totalNum, alarmNum) { var alarmPart = (alarmNum * ...
- FTP服务器访问主动模式、被动模式
在公司里面,经常需要访问外网FTP取资料等情况.但是有时用windows界面访问经常遇到各种问题. 下面介绍两种客户端是如何访问ftp服务器. 首先我们需要说明:防火墙,是阻拦外界与内部的通讯的一道关 ...
- flutter 环境搭建
环境: ladder什么的是必不可少的 win10 + Idea 2019.1.13 + Genymotion 2.12 基本可以在模拟器中运行项目,还有些许小问题,但是可以看到效果了 基本流程 下载 ...
- 【VIP视频网站项目】VIP视频网站项目v1.0.3版本发布啦(程序一键安装+电影后台自动抓取+代码结构调整)
在线体验地址:http://vip.52tech.tech/ GIthub源码:https://github.com/xiugangzhang/vip.github.io 项目预览 主页面 登录页面 ...
- enote笔记语言(4)(ver0.3)——“5w1h2k”分析法
章节:“5w1h2k”分析法 what:我想知道某个“关键词(keyword)”(即,词汇.词语,或称单词,可以是概念|专业术语|.......)的定义. why:我想知道事物发生的原因.“why ...
- char如何储存3个字节或者4个字节
1.char字符存储的是Unicode编码的代码点,也就是存储的是U+FF00这样的数值,然而我们在调试或者输出到输出流的时候,是JVM或者开发工具按照代码点对应的编码字符输出的. 2. 所以虽然UT ...
- Linux之iptables(四、网络防火墙及NAT)
网络防火墙 iptables/netfilter网络防火墙: (1) 充当网关 (2) 使用filter表的FORWARD链 注意的问题: (1) 请求-响应报文均会经由FORWARD链,要注意规则的 ...
- 00.pip安装包
pip安装更换镜像源 pip install 包名 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com pip导出和导入 ...
- git 的简单使用(3)
Git鼓励大量使用分支: 查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创建+切换分支:git ...