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

题意:你是空间站的一员,太空里有很多球形且体积不一的“小房间”,房间可能相距不近,也可能是接触或者甚至是重叠关系,所有的房间都必须相连,这样的话宇航员才能从这个房间走到另一个房间,而宇航员从一个房间走到另一个房间,只要满足三个条件中的一个即可:1两个房间是接触的,2两个房间是重叠的,3两个房间之间有走廊相连。也因此若是没有接触的两个小房间就要有走廊连接,忽略走廊的宽度,花费与长度成正比,所以当然是花费越少越好,而球与球之间的距离只接触到两球的表面即可,因为两球的表面相距最近,因此你的工作就是算给出的几个小房间中要达到相连的状态需花费的最小钱数是多少

思路:要求两个房间之间必须要有相连的走廊,所以就是最小生成树的思想,只要再考虑一下是不是接触或重叠就可以了

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring> using namespace std ; const double eps=1e- ;
const int INF = <<; struct point
{
double x,y,z,w ;
point() {}
point(double a,double b,double c,double d):x(a),y(b),z(c),w(d) {}
}; inline double sqrt1(double a)//函数被调用的次数多了就比较浪费时间,所以可以定义成内置函数
{
return a*a;
} double dis(const point &a,const point &b)//求两点间距离
{
return sqrt(sqrt1(a.x-b.x)+sqrt1(a.y-b.y)+sqrt1(a.z-b.z));
} double low[] ;
double dist[][],ans ;
bool vis[] ;
int n ; int prim()
{
ans = ;
int i,j,flag;
double minn ;
for(i = ; i <= n ; i++)
{
low[i] = INF ;
vis[i] = false ;
}
low[] = ;
for(i = ; i <= n ; i++)
{
minn = INF ;
flag = ;
for(j = ; j <= n ; j++)
{
if(minn > low[j]&&!vis[j])
{
minn = low[j] ;
flag = j ;
}
}
if(minn >= INF)
return false ;
ans += minn ;
vis[flag] = true ;
for(j = ; j <= n ; j++)
{
if(!vis[j]&&low[j]>dist[flag][j])
low[j] = dist[flag][j] ;
}
}
return true ;
} int main()
{
while(scanf("%d",&n)&&n)
{
point a[];
memset(dist,,sizeof(dist));
for(int i=; i<=n; i++)
scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z,&a[i].w);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(dis(a[i],a[j])-a[i].w-a[j].w<=)//两球间最短距离为两球球心距离再减去两球的半径
dist[i][j]=;
else if(dis(a[i],a[j])-a[i].w-a[j].w>eps)
dist[i][j]=dis(a[i],a[j])-a[i].w-a[j].w;
}
}
prim();
printf("%.3lf\n",ans);
}
return ;
}

特别郁闷的是明明是同一个代码,一开始交是0ms后来交就是16ms。。。。。

POJ2031Building a Space Station的更多相关文章

  1. POJ2031Building a Space Station (最小生成树之prim)

    Problem Description You are a member of the space station engineering team, and are assigned a task ...

  2. poj--2031--Building a Space Station(prime)

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

  3. poj2031-Building a Space Station(最小生成树,kruskal,prime)

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

  4. POJ 2031 Building a Space Station

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

  5. [ACM_搜索] POJ 1096 Space Station Shielding (搜索 + 洪泛算法Flood_Fill)

    Description Roger Wilco is in charge of the design of a low orbiting space station for the planet Ma ...

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

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

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

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

  8. Building a Space Station(kruskal,说好的数论呢)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3820   Accepted: 1950 Description You a ...

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

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

随机推荐

  1. OpenGL7-2-快速绘制

    代码下载 #include "CELLWinApp.hpp"#include <gl/GLU.h>#include <assert.h>#include & ...

  2. Smarty控制foreach输出数量

    最近在搞ecshop,ecshop用的是smarty.在首页中想要修改展示的商品数量,但是用的是foreach,让我无从下手,在网上找了好久终于找到解决方案. <!--{foreach name ...

  3. (转)Mysql数据库读写分离配置

    环境模拟 实现读写分离 减轻数据库的负荷 主服务器 master 10.0.0.12从服务器 slave 10.0.0.66 ------------------------------------- ...

  4. XPath 初步讲解

    2016-05-05 XPath是JavaScript 中节点查找手段,ie9以后的版本才支持w3c标准,其他浏览器基本支持.在e8之前的浏览器,通过基于 activeX的xml dom对象实现. 为 ...

  5. [转]PHP5.5安装PHPRedis扩展

    phpredis是个人觉得最好的一个php-redis客户端,因为其提供的function与redis的命令基本一致,降低的了学习成本,同时功能也很全面. 一.linux安装方法 phpredis下载 ...

  6. Linux dd 命令

    语法:dd [选项] if =输入文件(或设备名称). of =输出文件(或设备名称). ibs = bytes 一次读取bytes字节,即读入缓冲区的字节数. skip = blocks 跳过读入缓 ...

  7. 【Winform】无法嵌入互操作类型

    在使用Interop.SQLDMO进行数据库还原操作时,在vs2010编译时出现:无法嵌入互操作类型“……”,请改用适用的接口的解决方法 解决方案: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌 ...

  8. Spring Cloud OAuth

    In this blog post we will create a secure API for external access, using OAuth 2.0, to the microserv ...

  9. 使用XFire+Spring构建Web Service

    XFire是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,帮助你方便快速地开发Web Service应用. 相 对于Axis来说,目 ...

  10. python学习之js从0开始

    <html> <head> <title>js页面</title> <script src="js/old_boy.js"&g ...