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 ...
随机推荐
- 关键字 extern
定义:extern可置于变量或者函数前,以表示变量或者函数的定义在别的文件中.编译器会到其他模块中寻找其定义. extern int f(); extern int i; extern关键字 作为 ...
- color 颜色代码 android res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name=&q ...
- java在线截图---通过指定的URL对网站截图
如何以java实现网页截图技术 http://wenku.baidu.com/view/a7a8b6d076eeaeaad1f3305d.html http://blog.csdn.net/cping ...
- 解决FPDF报错:FPDF error: Not a JPEG file / FPDF error: Not a PNG file
最近有个项目需要用到FPDF,但是输出的时候报错: FPDF error: Not a JPEG file: http://***/data/attachment/forum/201603/19/10 ...
- xampp下安装yii框架下遇到的问题
用yii框架来生成web目录是输入E:\xampp\htdocs\yii\framework/yiic webapp E:\xampp\htdocs\web 时提示php不是内部命令,也不是... 这 ...
- 可变参数列表---以dbg()为例
在UART驱动的drivers/serial/samsung.h中遇到如下定义: #ifdef CONFIG_SERIAL_SAMSUNG_DEBUG extern void printascii(c ...
- Python生成验证码
#!/usr/bin/env python #coding:utf8 import random #方法1: str_code='zxcvbnmasdfghjklqwertyuiopZXCVBNMAS ...
- 半小时学会上传本地项目到github
半小时学会上传本地项目到github 闲着无聊写给那些正在学习怎么上传本地项目到github的同学. 开始学习 一.创建github账号 好吧,这步多余了. 二.创建个人仓库 三.配置SSH keys ...
- 【css】 收藏 纯css打造 mackbook air
http://www.cnblogs.com/myvin/p/4621231.html <html lang="en"> <head> <meta c ...
- 为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?
既然用Hibernate框架访问管理持久层,那为何又提到用Spring来管理以及整合Hibernate呢?把SessionFactory,Session,Transcational封装成包含crud的 ...