POJ2031Building a Space Station
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的更多相关文章
- POJ2031Building a Space Station (最小生成树之prim)
Problem Description You are a member of the space station engineering team, and are assigned a task ...
- poj--2031--Building a Space Station(prime)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6635 Accepte ...
- poj2031-Building a Space Station(最小生成树,kruskal,prime)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5874 Accepte ...
- POJ 2031 Building a Space Station
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- [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 ...
- 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 ...
- Building a Space Station(kruskal,说好的数论呢)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3820 Accepted: 1950 Description You a ...
- poj 2031 Building a Space Station【最小生成树prime】【模板题】
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5699 Accepte ...
随机推荐
- 译:Spring框架参考文档之IoC容器(未完成)
6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...
- 设置In_Memery
alter system set inmemory_size=4G scope=spfile; alter table table_name inmemory; alter table table_n ...
- Sublime Text 3下 Emmet 使用小技巧
Emmet常用技巧:(输入下面简写,按Tab键可触发效果) 生成 HTML 文档初始结构 html:5 或者 ! 生成 HTML5 结构 ...
- C#对象XML序列化
1.Xml序列化操作类 .Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象. 以下 ...
- DataX的简单编译安装测试
搭建环境: Java > =1.6 Python>=2.6 <3 Ant Rpmbuild G++ 编译DataX: 进入rpm文件夹 ...
- Adobe Photoshop CS4 Extended CS4 激活序列号
Adobe Photoshop CS4 Extended CS4 激活序列号(SN):1330-1779-4488-2103-6954-09161330-1170-1002-7856-5023-077 ...
- 一.Nginx的特性和一些知识点
一.Nginx的特性和一些知识点 1.基本功能服务器 处理静态文件(静态资源的web),支持 反向代理服务器,支持缓存.负载均衡.支持FastCGI 模块化机制,非DOS机制,支持 ...
- artdialog 提示 确定或取消
dialog({ title:'提示', content:"下载需扣除" + point + "个积分<br />重复下载不扣积分,需要继续吗?", ...
- dapper extensions (predicates)
https://github.com/tmsmith/Dapper-Extensions/wiki/Predicates The predicate system in Dapper Extensio ...
- java 中的匿名内部类
转自http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html 匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能 ...