这个题要交c++, 因为prime的返回值错了,改了一会

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

题意:就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通。如果两个球有重叠的部分则算为已连通,无需再搭桥。求搭建通路的最小费用(费用就是边权,就是两个球面之间的距离

思路:求球之间的距离, 距离小于半径 说明联通 , 距离为0.

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
double const INF=<<;
double G[][]; struct node
{
double x,y,z,r;
}p[];
double prime(int n)
{
double sum=0.0;
int i,j,pos;
int v[]={};
double d[],min;
for(i=; i<=n; i++)
d[i]=G[][i];
v[]=;
for(i=; i<=n; i++)
{
min=INF;
for(j=; j<=n; j++)
{
if(!v[j]&&min>d[j])
{
min=d[j];
pos=j;
}
}
sum+=min;
v[pos]=;
//printf("%.3lf %.3lf\n",min,sum);
for(j=; j<=n; j++)
{
if(!v[j])
{
if(d[j]>G[pos][j])
d[j]=G[pos][j];
}
}
//printf("%.3lf %.3lf\n",min,sum);
}
//printf("%.3lf\n",sum);
return sum;
}
int main()
{
int n,i,j;
double d,sum;
while(scanf("%d",&n)&&n)
{
memset(p,,sizeof(p));
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
G[i][j]=INF;
G[i][i]=;
}
for(i=; i<=n; i++)
{
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z,&p[i].r);
for(j=; j<i; j++)
{
d=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y)
+(p[i].z-p[j].z)*(p[i].z-p[j].z));
if(d-p[i].r-p[j].r<=)
G[i][j]=G[j][i]=;
else
G[i][j]=G[j][i]=d-p[i].r-p[j].r;
}
}
sum=prime(n);
printf("%.3lf\n",sum);
}
return ;
}

poj 2031 Building a Space Station(prime )的更多相关文章

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

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

  2. POJ - 2031 Building a Space Station 【PRIME】

    题目链接 http://poj.org/problem?id=2031 题意 给出N个球形的 个体 如果 两个个体 相互接触 或者 包含 那么 这两个个体之间就能够互相通达 现在给出若干个这样的个体 ...

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

    http://poj.org/problem?id=2031 题意 给出三维坐标系下的n个球体,求把它们联通的最小代价. 分析 最小生成树加上一点计算几何.建图,若两球体原本有接触,则边权为0:否则边 ...

  4. poj 2031 Building a Space Station(最小生成树,三维,基础)

    只是坐标变成三维得了,而且要减去两边的半径而已 题目 //最小生成树,只是变成三维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> ...

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

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

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

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

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

  8. POJ 2031 Building a Space Station

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

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

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

随机推荐

  1. php截取字符串的实例代码(支持utf-8)

    分享下php中截取字符串的例子,支持utf-8格式. 1,截取字符串 <?php $string="2006年4月我又长大了一岁!"; echo substr($string ...

  2. 手机淘宝用JS来动态写meta标签(1像素边框处理方法)

    var metaEl = doc.createElement('meta'); var scale = isRetina ? 0.5:1; metaEl.setAttribute('name', 'v ...

  3. QQ聊天机器人for PHP版 (登录,收、发消息)

    <?php include "http_no_cookie.class.php"; class qq { public $sid; public $http; public ...

  4. 手工添加Linux防火墙端口

    在linux实际操作中经常需要对防火墙进行修改,比如经常要修改或添加相关端口,下面以添加运行Tomcat所需8080端口为例: (以下命令操作均为root用户) 1.编辑iptables文件 # vi ...

  5. 五、mysql存储引擎

    show variable like 'table_type'; 显示系统默认存储引擎 show engine\G 显示系统支持存储殷勤 =============================== ...

  6. 如何让VS根据编译环境选择相应的配置文件

    其实微软还是蛮有创造力的,一个配置文件居然弄了这么多的形式,从原来的ini到现在的xml,总而言之让我们这些在微软殿堂里的程序员翘着屁股追赶. 微软最新的配置文件实际上就是个xml文件,以后缀名.co ...

  7. java第六课 oop

    java oop 1.面向过程的结构化程序设计弊端:方法和数据结构都是毫无规律的定义在程序中任何位置        方法定义和方法要处理的数据结构也都是分开定义 2.对象:每new一次,就创建1个新对 ...

  8. this.Invoke和this.BeginInvoke的区别

    private void button1_Click(object sender, EventArgs e) { "; this.Invoke(new EventHandler(delega ...

  9. Shiro 缓存失效以后的一个问题

    shiro 1.2.2和1.2.3 为shiro设置了缓存,但是当服务器运行几个小时后,页面判断 <shiro:hasPermission name="admin"> ...

  10. eclipse中切换jre后报错:Java compiler level does not match the version of the installed Java project facet.

    项目移除原来的jre环境lib后,添加本地的jre,报错如下: Java compiler level does not match the version of the installed Java ...