题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r。如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树。

题目思路:最小生成树树模板过了,没啥说的

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1005 using namespace std; double x[MAX],y[MAX],z[MAX],r[MAX],Map[MAX][MAX],dist[MAX];
int n,vis[MAX]; double Dist(int a,int b)//计算两球心距离
{
double k1=x[a]-x[b];
double k2=y[a]-y[b];
double k3=z[a]-z[b];
return sqrt(k1*k1+k2*k2+k3*k3);
} void prm()
{
int i,j,k;
double minn,sum=;
vis[]=;
for(i=;i<=n;i++)
dist[i]=Map[][i];
for(i=;i<n;i++)
{
minn=INF;
for(j=;j<=n;j++)
{
if(!vis[j]&&dist[j]<minn)
{
minn=dist[j];
k=j;
}
}
if(minn==INF)
break;
sum+=dist[k];
vis[k]=;
for(j=;j<=n;j++)
{
if(Map[k][j]<dist[j] && !vis[j])
{
dist[j]=Map[k][j];
}
}
}
printf("%.3lf\n",sum);
} int main()
{
int i,j;
while(scanf("%d",&n),n)
{
for(i=;i<MAX;i++)
for(j=;j<MAX;j++)
Map[i][j]=INF;
memset(vis,,sizeof(vis));
memset(dist,,sizeof(dist));
for(i=;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x[i],&y[i],&z[i],&r[i]);
}
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
if(i!=j)
{
double d=Dist(i,j);
if(d>(r[i]+r[j]))
{
Map[i][j]=Map[j][i]=min(Map[i][j],(d-r[i]-r[j]));
}
else
{
Map[i][j]=Map[j][i]=;
}
}
}
}
prm();
}
return ;
}

POJ 2031 Building a Space Station 最小生成树模板的更多相关文章

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

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

  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【经典最小生成树】

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

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

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

  5. POJ 2031 Building a Space Station

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

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

  7. POJ 2031 Building a Space Station (计算几何+最小生成树)

    题目: Description You are a member of the space station engineering team, and are assigned a task in t ...

  8. POJ 2031 Building a Space Station【最小生成树+简单计算几何】

    You are a member of the space station engineering team, and are assigned a task in the construction ...

  9. [ An Ac a Day ^_^ ][kuangbin带你飞]专题六 最小生成树 POJ 2031 Building a Space Station

    最小生成树模板题 注意最后输出用%f (从C99开始%f已经不能用于输出double 即 输入用%lf 输出用%f) #include<cstdio> #include<algori ...

随机推荐

  1. C# delegate的匿名方法

  2. POJ 2773 Happy 2006#素数筛选+容斥原理+二分

    http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1, ...

  3. 浙大 pat 1023题解

    1023. Have Fun with Numbers (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. 关于CSS样式的那些事_导航条菜单讲解

    最近开始忙着开自己的个人博客了,自己的前端确实是渣渣.没办法,一步步来,从慕课网上慢慢学着先. 首先带来的是一个导航栏的设计: 垂直导航栏的设计: 直接上代码: <!DOCTYPE html P ...

  5. AnimatorController动画遮罩

    游戏中,我们的人物在某些时候,可能在一个时间点在做两件事,这样就需要同时播放两个动画,但我们肯定不能简单将其叠加,比如移动着开枪.攻击.挥手等等,那么在Unity中我们应该怎样实现这样的功能呢?且听我 ...

  6. 应用 Valgrind 发现 Linux 程序的内存问题(转)

    Valgrind 概述 体系结构 Valgrind 是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个 ...

  7. WTL消息以及处理函数声明

    MSG_WM_CREATE LRESULT OnCreate(LPCREATESTRUCT lpCreateStruct); MSG_WM_INITDIALOG LRESULT OnInitDialo ...

  8. pc app 桌面打包

    进入 http://nwjs.io/  下载 创建web项目,在项目根目录 创建文件package.json并填写 1 2 3 4 5 6 7 {   "name": " ...

  9. Chapter 15_3 使用环境

    创建模块的基本方法的缺点在于,忘记使用local,很容易就污染全局空间. “函数环境”是一种有趣的技术,它能够解决上面的问题.就是让模块的主程序块独占一个环境. 这样不仅它的所有函数可以共享这个tab ...

  10. mvc路由参数注解

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //过滤掉禁止访问的路由 routes.MapRoute( name: &quo ...