POJ - 2031 Building a Space Station(计算几何+最小生成树)
http://poj.org/problem?id=2031
题意
给出三维坐标系下的n个球体,求把它们联通的最小代价。
分析
最小生成树加上一点计算几何。建图,若两球体原本有接触,则边权为0;否则边权为它们球心的距离-两者半径之和。这样来跑Prim就ok了。注意精度。
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define lc idx<<1
#define rc idx<<1|1
#define rson mid+1,r,rc
#define lson l,mid,lc
using namespace std;
typedef long long ll;
template <class T>
void test(T a) {
cout<<a<<endl;
}
template <class T,class T2>
void test(T a,T2 b) {
cout<<a<<" "<<b<<endl;
}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c) {
cout<<a<<" "<<b<<" "<<c<<endl;
}
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = 1e9+;
int T;
void testcase() {
printf("Case %d: ",++T);
}
const int MAXN = 1e5+;
const int MAXM = ;
const double PI = acos(-1.0);
const double eps = 1e-;
struct node{
double x,y,z,r;
}p[];
double dist(node a,node b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
double g[][],lowc[];
bool vis[];
int main() {
#ifdef LOCAL
freopen("data.in","r",stdin);
#endif // LOCAL
int n;
while(~scd(n)&&n){
for(int i=;i<n;i++){
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z,&p[i].r);
}
double maxx=0.0;
for(int i=;i<n;i++){
g[i][i]=0.0;
for(int j=i+;j<n;j++){
if(dist(p[i],p[j])-(p[i].r+p[j].r)<=eps){
g[i][j]=g[j][i]=0.0;
}else{
g[i][j]=g[j][i]=dist(p[i],p[j])-(p[i].r+p[j].r);
maxx=max(maxx,g[i][j]);
}
}
}
mset(vis,false);
double ans=;
vis[]=true;
for(int i=;i<n;i++) lowc[i]=g[][i];
for(int i=;i<n;i++){
double minc = maxx;
int p=-;
for(int j=;j<n;j++){
if(!vis[j]&&minc>lowc[j]){
minc=lowc[j];
p=j;
}
}
ans+=minc;
vis[p]=true;
for(int j=;j<n;j++){
if(!vis[j]&&lowc[j]-g[p][j]>eps){
lowc[j]=g[p][j];
}
}
} printf("%.3f\n",ans);
}
return ;
}
POJ - 2031 Building a Space Station(计算几何+最小生成树)的更多相关文章
- 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 (最小生成树)
Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...
- 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【最小生成树+简单计算几何】
You are a member of the space station engineering team, and are assigned a task in the construction ...
- poj 2031 Building a Space Station(最小生成树,三维,基础)
只是坐标变成三维得了,而且要减去两边的半径而已 题目 //最小生成树,只是变成三维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> ...
- 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 (最小生成树)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5173 Accepte ...
- POJ 2031 Building a Space Station
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- 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 (prim裸题)
Description You are a member of the space station engineering team, and are assigned a task in the c ...
随机推荐
- 基于Windows Subsystem for Linux (WSL) 【Ubuntu】在WIN10 Home Edition安装Docker
root@Andy-PC:~# uname -a Linux Andy-PC --Microsoft #-Microsoft Fri Apr :: PST x86_64 x86_64 x86_64 G ...
- 详解centos6和centos7防火墙的关闭
http://www.jb51.net/article/101576.htm http://www.myhack58.com/Article/48/66/2013/37314.htm http://w ...
- node的router路由。
使用router可以实现一个小型的express. router继承了大部分的app = express()的方法. 使用router可以工程化管理项目.router使用以后app只能在最开始去存在. ...
- SQLSERVER最简单的同名数据库恢复过程.
一. 冷备份恢复 1. net stop mssqlserver # 如果是安装的默认数据库实例 关闭 sqlserver的数据库 2. copy sqlserver的数据文件 主要是mdf 数据文件 ...
- SQLSERVER 2014 内存优化表相关
更新了SP2的补丁能够解决 不能收缩日志文件的bug了. 但是因为已经不用内存优化表了, 所以想着能够删除内存优化表的file group 但是发现 很难删除 先说结论: 以下是针对内存优化文件组的 ...
- const修饰符限定的常量
类型前加const修饰符限定变量为只读,称为常量,定义时必须初始化,且初始化后编译器不允许再修改常量的值. 一.常量的定义 const在类型前面 const int value: //value是co ...
- Laravel Eloquent ORM 时如何查询表中指定的字段
导读:在使用Laravel ORM的Model方法find, get, first方法获取数据对象时返回的数据对象的attributes属性数组里会包含数据表中所有的字段对应...原文地址:http: ...
- js 消息框
消息框有三種:警告框.提示框.確認框 警告框: 確保用戶可以得到某些信息. 點擊確定以後,才能繼續後續操作. alert("不好") 確認框: 請用戶確認或者接受某些信息. 點擊確 ...
- python之pygal:掷一个骰子统计次数并以直方图形式显示
源码如下: # pygal包:生成可缩放的矢量图形文件,可自适应不同尺寸的屏幕显示 # 安装:python -m pip intall pygal-2.4.0-py2.py3-none-any.whl ...
- BZOJ3835[Poi2014]Supercomputer——斜率优化
题目描述 Byteasar has designed a supercomputer of novel architecture. It may comprise of many (identical ...