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(计算几何+最小生成树)的更多相关文章

  1. POJ 2031 Building a Space Station (计算几何+最小生成树)

    题目: Description You are a member of the space station engineering team, and are assigned a task in t ...

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

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

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

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

  4. POJ 2031 Building a Space Station【最小生成树+简单计算几何】

    You are a member of the space station engineering team, and are assigned a task in the construction ...

  5. poj 2031 Building a Space Station(最小生成树,三维,基础)

    只是坐标变成三维得了,而且要减去两边的半径而已 题目 //最小生成树,只是变成三维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> ...

  6. POJ 2031 Building a Space Station【经典最小生成树】

    链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

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

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

  8. POJ 2031 Building a Space Station

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

  9. 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 ...

  10. 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 ...

随机推荐

  1. js原生常用事件event

    onblur 元素失去焦点: onchange用户改变域的内容: onclick鼠标点击对象: onerror当加载图片时发生错误: onfocus 元素获得焦点: onkeypress某个键盘的键被 ...

  2. Windows环境搭建mysql服务器

    Windows环境搭建mysql服务器: 1.下载mysql-installer-community-5.7.3.0-m13.2063434697并安装  安装详细步骤>> 2.安装mys ...

  3. [转载]Memory Limits for Windows and Windows Server Releases

    Memory Limits for Windows and Windows Server Releases This topic describes the memory limits for sup ...

  4. [日常工作] Linux与Windows的连接访问以及数据共享等方法 vncserver smb xshell xftp winscp mount等

    日常办公机器是用 windows, 但是越来越多的测试和工作需求需要使用linux. 这里以最常用的系统centos为例进行说明 1. 远程连接 ssh的方式 建议使用xmange 系列的 xshel ...

  5. 关于更改ListBox的ItemsPanel样式

    首先定义一个ListBoxItem的样式,用来显示相应的图片信息 <Style TargetType="{x:Type ListBoxItem}" > <Sett ...

  6. Delphi编程中动态菜单要点归纳

      一.创建菜单并添加项目 在设计程序时,有时需要动态创建菜单, 通常使用以下的语句: PopupMenu1 := TPopupMenu.Create(Self);  Item := TMenuIte ...

  7. python之tkinter使用-滚动条

    # GUI:tkinter使用 # 通过调节滚动条改变标签中字体大小 import tkinter as tk def resize(ev=None): '''改变label字体大小''' label ...

  8. BZOJ4943 NOI2017蚯蚓排队(哈希+链表)

    能看懂题就能想到正解.维护所有长度不超过k的数字串的哈希值即可,用链表维护一下蚯蚓间连接情况.由于这样的数字串至多只有nk个,计算哈希值的总复杂度为O(nk),而分裂的复杂度为O(ck^2),询问复杂 ...

  9. spoj COT - Count on a tree (树上第K小 LCA+主席树)

    链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...

  10. day30 __hash__ 计算哈希值

    hash() # __hash__哈希的时候会根据内存地址进行哈希,因为地址不同所以哈希的值也不同,哪怕是完全一样子的属性得出的哈希值也不一样因此存在需要某些时刻期望属性相同得出相同哈希值可以控制对象 ...