【POJ】2031 Building a Space Station
题目链接:http://poj.org/problem?id=2031
题意:修建太空站每个舱之间的走廊。每个舱都是球体。给出n个舱的三维空间坐标以及球体半径。如果球体之间接触或者相接,就不用修走廊。让你求最短走廊的长度。
题解:有点点坑这个题。。改了好久。。这里的存储其实是 $len(a,b) - r_a - r_b$ 求一下这个存储再以Prim求解即可。注意精度。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
double mp[maxn][maxn];
double dis[maxn];
bool vis[maxn];
int n; struct node{
double x;
double y;
double z;
double r;
}; node cir[maxn]; double dist(node a,node b){
double diss = (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z -b.z) * (a.z - b.z);
return sqrt(diss) - a.r - b.r;
} double prim(){
double ans = ;
int k;
double minn ;
for(int i = ; i <= n ; i++){
dis[i] = inf ;
vis[i] = false ;
}
dis[] = ;
for(int i = ; i <= n ; i++){
minn = inf ;
k = ;
for(int j = ; j <= n ; j++){
if(minn > dis[j] && !vis[j]){
minn = dis[j] ;
k = j ;
}
}
if(minn >= inf)
return - ;
ans += minn ;
vis[k] = true ;
for(int j = ; j <= n ; j++){
if(!vis[j] && dis[j] > mp[k][j])
dis[j] = mp[k][j] ;
}
}
return ans ;
} int main(){
while(cin>>n && n){
for(int i = ; i <= n ;i++){
cin>>cir[i].x>>cir[i].y>>cir[i].z>>cir[i].r;
}
memset(mp,,sizeof(mp));
for(int i = ; i < n; i++){
for(int j = i+; j <= n ;j++){
if(dist(cir[i],cir[j]) < eps){
mp[i][j] = mp[j][i] = ;
}
else{
mp[i][j] = mp[j][i] = dist(cir[i],cir[j]);
}
}
}
/*
for(int i = 1; i <= n; i++){
for(int j = 1 ; j <= n ;j++){
cout<<mp[i][j]<<" ";
}
cout<<endl;
}*/
double ans = prim();
if(ans != -){
printf("%.3lf\n",ans);
} } return ;
}
【POJ】2031 Building a Space Station的更多相关文章
- POJ 2031 Building a Space Station【经典最小生成树】
链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- poj 2031 Building a Space Station【最小生成树prime】【模板题】
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5699 Accepte ...
- POJ 2031 Building a Space Station
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- 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 ...
- 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 ...
- POJ 2031 Building a Space Station【最小生成树+简单计算几何】
You are a member of the space station engineering team, and are assigned a task in the construction ...
- POJ 2031 Building a Space Station (计算几何+最小生成树)
题目: Description You are a member of the space station engineering team, and are assigned a task in t ...
- POJ 2031 Building a Space Station (prim裸题)
Description You are a member of the space station engineering team, and are assigned a task in the c ...
随机推荐
- 7.Jmeter 快速入门教程--录制复杂web测试脚本
Jmeter的功能简单,不需要有脚本语言的编写经验,纯图形界面添加测试场景, 用起来上手很快.但是如果手动添加每一个web(http/https)请求,费时又费力.而且有可能最后手动编写的和实际发的请 ...
- LINUX 安装PHP GD库遇到的坑
本文借鉴:https://www.cnblogs.com/gaohj/p/3152646.html linux下为php添加GD库的步骤如下: 一.下载 gd-.tar.gz http://www.b ...
- strtotime 获取当月最后一天的日期
strtotime('last day of this month', $timestamp);
- 分布式-技术专区-Redis分布式锁原理实现
在很多场景中,我们为了保证数据的最终一致性,需要很多的技术方案来支持,比如分布式事务.分布式锁等.那具体什么是分布式锁,分布式锁应用在哪些业务场景.如何来实现分布式锁呢?今天来探讨分布式锁这个话题. ...
- Raspberry Pi 4B 安装QT5和qtCreator
https://blog.csdn.net/coekjin/article/details/52049273 sudo apt-get install qt5-default sudo apt-get ...
- go之linux安装
下载地址:https://golang.org/dl/ ubuntu16.04安装过程 官方安装文档:https://golang.org/doc/install?download=go1.11.4. ...
- 带你看懂LayoutInflater中inflate方法
关于inflate问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东 ...
- shell变量替换扩展 字符串计数截取
- 历史上最详细的SpringCloud搭建微服务的过程。(包括注册中心,服务提供者和服务消费者)
首先搭建注册中心,创建一个springboot的maven工程. 工程创建完成之后,先在资源文件中的application.properties中写配置文件. server.port= spring. ...
- leetcode-162周赛-1254-统计封闭岛屿数量
题目描述: 自己的提交: class Solution: def closedIsland(self, grid: List[List[int]]) -> int: def dfs(grid,r ...