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. 获取qq音乐json数据---某课网音乐app学习

    移动端qq音乐地址:https://m.y.qq.com/ .抓取QQ音乐数据 请求首页时,有如下链接,回调了jsonp https://c.y.qq.com/splcloud/fcgi-bin/p. ...

  2. lamlmzhang的新博客开通了,欢迎大家的关注

    从这里开始lamlmzhang的java开发之路~!

  3. jstree CHECKBOX PLUGIN

    The checkbox plugin makes multiselection possible using three-state checkboxes. Configuration overri ...

  4. vivado2018.3 与 modelsim联合仿真

    我用的是目前最新版本的软件,vivado2018.3与modelsim10.6d.废话不多说,直接上操作 1.modelsim编译vivado库 1)双击启动vivado软件,如下图操作 2)Simu ...

  5. POJ——3169Layout(差分约束)

    POJ——3169Layout Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14702   Accepted ...

  6. 标准C 语言总结

    ***************C语言****************** --day01-- Linux是一个和Windows类似的操作系统 通常通过终端软件使用Linux操作系统 终端软件里只能使用 ...

  7. Nginx配置文件的高亮显示设置

    linux系统下vim或者vi编辑器默认是没有对nginx的语法高亮设置. 1.下载vi语法高亮配置到 ~/.vim/syntax,如果不存在则创建该目录,cd ~/.vim/syntax wget ...

  8. =、==、is、id(内容)

    = 赋值 == 比较值是否相等 is 比较.比较的是内存地址 id(内容) 测出内存地址

  9. Windows编译PHP7.2拓展

    转载请注明文章出处:https://tlanyan.me/windows-co... 准备工作 https://github.com/Microsoft/...下载PHP-SDK(在右边的" ...

  10. python下操作mysql 之 pymsql

    python下操作mysql  之  pymsql pymsql是Python中操作MySQL的模块, 下载安装: pip3 install pymysql 使用操作 1, 执行SQL #!/usr/ ...