Building a Space Station POJ - 2031 三维最小生成树,其实就是板子题
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N=1e5;
struct edge{
int a,b;
double w;
}e[N];
double x[N],y[N],z[N],r[N];
int n,tot,p[N];
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
void kruskal()
{
double sum=;
for(int i=;i<tot;i++)
{
int a=find(e[i].a);
int b=find(e[i].b);
double w=e[i].w;
if(a!=b)
{
sum+=w;
p[a]=b;
}
}
printf("%.3f\n",sum);
}
int main()
{
while(cin>>n&&n)
{
for(int i=;i<n;i++)
cin>>x[i]>>y[i]>>z[i]>>r[i];
for(int i=;i<=n;i++)
p[i]=i;
tot=;
for(int i=;i<n;i++)
for(int j=i+;j<n;j++)
{
double t=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+(z[i]-z[j])*(z[i]-z[j]));
if(r[i]+r[j]>=t)
e[tot++]={i,j,};
else
e[tot++]={i,j,t-r[i]-r[j]};
}
sort(e,e+tot,cmp);
kruskal(); }
return ;
}
Building a Space Station POJ - 2031 三维最小生成树,其实就是板子题的更多相关文章
- Building a Space Station POJ 2031 【最小生成树 prim】
http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...
- Building a Space Station POJ - 2031
Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...
- (最小生成树) Building a Space Station -- POJ -- 2031
链接: http://poj.org/problem?id=2031 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6011 ...
- C - Building a Space Station - poj 2031
空间站是有一些球状的房间组成的,现在有一些房间但是没有相互连接,你需要设计一些走廊使他们都相通,当然,有些房间可能会有重合(很神奇的样子,重合距离是0),你需要设计出来最短的走廊使所有的点都连接. 分 ...
- POJ-2031 Building a Space Station (球的最小生成树)
http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...
- 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: 6083 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【最小生成树prime】【模板题】
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5699 Accepte ...
随机推荐
- Github搜索技巧-如何使用github找到自己感兴趣的项目(转载)
Github现在不仅仅作为一个版本控制工具,更是一个开源的仓库,里面不但有优秀的开源代码,电子书,还有一些五花八门的项目,有些国家的法律也放在上面,作为程序员如何找到自己感兴趣的项目就非常重要了! 欢 ...
- Hanoi塔问题——递归
/////////////Hanoi塔问题///////#include<iostream>using namespace std;void hanoi(int i,char A,char ...
- 重读es6, 正确了解promise中catch的用法
前言 在最近的项目中,用到了es6的promise语法,发现promise.prototype.catch 并不只是单单reject抛出的回调函数,所以今天做一些笔录,防止以后在项目中又碰到这样的问题 ...
- Zookeeper 部署 配置文件
Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式. ■ 单机模式: Zookeeper只运行在一台服务器上,适合测试环境:■ 伪集群模式:就是 ...
- Django (一) 基础
创建项目 创建app python manager.py startapp app01 修改.添加url from django.conf.urls import url,include fr ...
- 维基逃离MySQL 力挺开源数据库 MariaDB
近日全球著名百科类网站维基百科宣布,将不会再用MySQL数据库,据国外媒体报道,很多年,MySQL一直是热门的开源数据库,不过在被甲骨文收购后,面临闭源的风险.因此维基百科将切换到另外一款开源数据库M ...
- php oci 和 pdo_oci 安装
安装非常复杂,必须记录 CentOS服务器上已有相关环境:apache.php5 需要安装:1.oracle客户端.2.oci8扩展.3.pdo_oci扩展. 一. 准备文件 1) oracle客户端 ...
- Angular路由使用
一. 路由:根据不同URL地址,动态让根组件挂载其他组件来实现单页面应用,相对地址 1. 项目一开始创建就会询问是否添加路由(Angular routing) 2. 有无路由区别{ 1. 多了一个ro ...
- 【转载】Java的Vector,ArrayList,LinkedList
首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...
- Flutter Widgets 之 InkWell 和 Ink
注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 InkWell InkWell组件在用户点击时出现&quo ...