poj 2031 Building a Space Station(最小生成树)

【代码超详解】POJ 2031 Building a Space Station(Kruskal 算法求最小生成树) 一、题目描述 Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16773 Accepted: 7429(2020/3/20 14:42) Description You are a member of the space station engineering team, a... 阅读详情

Description

You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task. 
The space station is made up with a number of units, called cells. All cells are sphere-shaped, but their sizes are not necessarily uniform. Each cell is fixed at its predetermined position shortly after the station is successfully put into its orbit. It is quite strange that two cells may be touching each other, or even may be overlapping. In an extreme case, a cell may be totally enclosing another one. I do not know how such arrangements are possible.

All the cells must be connected, since crew members should be able to walk from any cell to any other cell. They can walk from a cell A to another cell B, if, (1) A and B are touching each other or overlapping, (2) A and B are connected by a `corridor', or (3) there is a cell C such that walking from A to C, and also from B to C are both possible. Note that the condition (3) should be interpreted transitively. 

You are expected to design a configuration, namely, which pairs of cells are to be connected with corridors. There is some freedom in the corridor configuration. For example, if there are three cells A, B and C, not touching nor overlapping each other, at least three plans are possible in order to connect all three cells. The first is to build corridors A-B and A-C, the second B-C and B-A, the third C-A and C-B. The cost of building a corridor is proportional to its length. Therefore, you should choose a plan with the shortest total length of the corridors. 

You can ignore the width of a corridor. A corridor is built between points on two cells' surfaces. It can be made arbitrarily long, but of course the shortest one is chosen. Even if two corridors A-B and C-D intersect in space, they are not considered to form a connection path between (for example) A and C. In other words, you may consider that two corridors never intersect. 

Input

The input consists of multiple data sets. Each data set is given in the following format. 

n 
x1 y1 z1 r1 
x2 y2 z2 r2 
... 
xn yn zn rn 

The first line of a data set contains an integer n, which is the number of cells. n is positive, and does not exceed 100. 

The following n lines are descriptions of cells. Four values in a line are x-, y- and z-coordinates of the center, and radius (called r in the rest of the problem) of the sphere, in this order. Each value is given by a decimal fraction, with 3 digits after the decimal point. Values are separated by a space character. 

Each of x, y, z and r is positive and is less than 100.0. 

The end of the input is indicated by a line containing a zero. 

Output

For each data set, the shortest total length of the corridors should be printed, each in a separate line. The printed values should have 3 digits after the decimal point. They may not have an error greater than 0.001. 

Note that if no corridors are necessary, that is, if all the cells are connected without corridors, the shortest total length of the corridors is 0.000. 

Sample Input

3
10.000 10.000 50.000 10.000
40.000 10.000 50.000 10.000
40.000 40.000 50.000 10.000
2
30.000 30.000 30.000 20.000
40.000 40.000 40.000 20.000
5
5.729 15.143 3.996 25.837
6.013 14.372 4.818 10.671
80.115 63.292 84.477 15.120
64.095 80.924 70.029 14.881
39.472 85.116 71.369 5.553
0

Sample Output

20.000
0.000
73.834

Source



题意:几个球的最小生成树,如果球的接触那么距离为0,上代码了


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 1005

struct stud{
double x,y,z,r;
}s[N];

struct studd{
int a,b;
double len;
}f[N*N];

int n,k;
int father[N];


int cmp(studd a,studd b)
{
    return a.len<b.len;
}

int cha(int x)
{
    if(x!=father[x])
        father[x]=cha(father[x]);
    return father[x];

}

int main()
{
    int i,j;
    while(scanf("%d",&n),n)
    {
        for(i=0;i<n;i++)
            scanf("%lf%lf%lf%lf",&s[i].x,&s[i].y,&s[i].z,&s[i].r);

        k=0;

        for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
        {
            f[k].a=i;
            f[k].b=j;
            double x=s[i].x-s[j].x;
            double y=s[i].y-s[j].y;
            double z=s[i].z-s[j].z;
            double ll=sqrt(x*x+y*y+z*z)-s[i].r-s[j].r;

            if(ll<0)
                f[k].len=0;
            else
                f[k].len=ll;
            k++;
        }

        sort(f,f+k,cmp);

        for(i=0;i<=n;i++)
            father[i]=i;

        int num=1;

        double ans=0;

        for(i=0;i<k;i++)
        {
            int aa=cha(f[i].a);
            int bb=cha(f[i].b);
            if(aa!=bb)
            {
                father[aa]=bb;
                ans+=f[i].len;
                num++;
                if(num==n)
                    break;
            }
        }

        printf("%.3f\n",ans);
    }
    return 0;
}


POJ2031-Building a Space Station【Prim+计算几何】 北大POJ2031-Building a Space Station【Prim+计算几何】 POJ2031-Building a Space Station【Prim+计算几何】 立即下载

相关推荐

音视频从入门到精通——指数哥伦布码原理

在计算机中,一般数字的编码都为二进制,但是由于以相等长度来记录不同数字,因此会出现很多的。

mirana的专栏 1380

POJ 2031 Building a Space Station(Kruskal算法)

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2253   Accepted: 1203 Descr

qiyazhuang的专栏 799

从设计到实战:手把手教你用SolidWorks完成一个简易离心泵叶轮的水力设计全流程

本文详细介绍了使用SolidWorks进行简易离心泵叶轮水力设计的全流程,涵盖参数计算、水力设计和三维建模等关键步骤。通过实际案例演示,帮助机械设计工程师和学生掌握从理论到实践的转化技巧,特别适合低比转速离心泵叶轮的设计需求。

weixin_30621711的博客 230

Building a Space Station (最小生成树 Kruskal)

Building a Space Station 题目描述: You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task. The space station is made up with

Eagle222的博客 313

【实验】Linux下完全卸载oracle单实例(亲身体验完整版)

停止oracle数据库服务 停止监听 卸载数据库实例 因为自己懒得再弄dbca图形化界面啥的,直接采用dbca静默删除数据库实例,命令如下 dbca -silent -deleteDatabase -sourcedb orclhq -sid orcl -sysDBAUserName orcl -sysDBAPassword zte [oracle@slave ~]$ dbca -silent ...

萝卜吃鱼914的博客 1945

银河麒麟V10 SP1忘记密码?3分钟搞定单用户模式密码重置(附详细截图)

本文详细介绍了银河麒麟V10 SP1操作系统忘记登录密码后的应急恢复方法。核心是通过GRUB引导菜单进入单用户模式,无需密码即可获得root权限,从而快速重置用户密码。文章提供了从捕捉GRUB界面、编辑启动参数到执行密码修改命令的完整步骤图解与常见问题解决方案,帮助用户在几分钟内恢复系统访问。

w3x4y的博客 913

poj2031——Building a Space Station【最小生成树】

Building a Space StationTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 11514 Accepted: 5282DescriptionYou are a member of the space station engineering team, and are assigned a task in the ...

xiang_hehe的博客 260

利用LTspice设计Pt100测温电路

是一种广泛使用的温度传感器,它属于(RTD, Resistance Temperature Detector)的类型,通常用于温度测量。在0°C时,的电阻值为100欧姆,且它的电阻值会随着温度的升高而升高。PT100热电阻温度阻值对应表)因此可以根据这一特性来检测出环境温度。

2301_77033628的博客 2412

poj j2031(几何+最小生成树)

计算各球心的距离,小于半径之和的算0,简单的几何计算+kruskal  注意double的快排调用时   wa了n次       #include #include #include #include #include using namespace std; int parent[10000]; int k; struct edge { int u,

hcy的专栏 353

PCIe-Orders set Blocks之symbol对齐

有序集的使用场合很多,如在链路link阶段,进入polling后,便开始有序集的发送。一个 Ordered Set Block 包含一个同步头,后跟一个有序集。多通道链路的所有 Lane 必须同时传输相同的有序集,而且有序集的第一个符号定义了有序集的类型。但在PCIE中,8GT/s及以上的速率,采用的是128b/130b编码。128-bit是想要传输的payload,实际会传输130-bit,多出来的2-bit就是sync header。Block有两种,通过Sync header区分:10b:表示data

weixin_44700185的博客 813

YOLOv11 改进 - 注意力机制 | EMA (Efficient Multi-Scale Attention) 高效多尺度注意力:跨空间学习与多分支协同增强特征表征,优化多尺度目标检测

本文介绍了高效多尺度注意力(EMA)模块及其在YOLOv11中的结合应用。现有注意力机制在通道维度缩减时可能影响深度视觉表示,EMA模块通过结合通道和空间信息、采用多尺度并行子网络结构等创新点,实现了高效的多尺度注意力机制。其基本原理包括通道和空间注意力结合、多尺度并行子网络设计等。在多个流行基准数据集上的实验表明,该模块能有效提高特征表示能力。我们将EMA集成进YOLOv11,实验证明改进后的模型在目标检测任务中性能优越,具有广泛应用前景

魔改工程师的博客 1292

(最小生成树)POJ2031Building a Space Station

POJ2031Building a Space Station 题意&思路: 给你n个球的球心坐标(x,y,z)和半径r,如果两个球相交则视为连通。问使所有球两两连通(表面接触)的最短道路。 模板题。注意点就是两两连通就是两个球的距离设为0,以及提交时用C++才能交%lf。 代码: #include<iostream> #include<stdio.h> #incl...

Z7784562的博客 199

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

【POJ 2031】 Building a Space Station (三维坐标求距离+最小生成树)

【POJ 2031】 Building a Space Station n个空间球 可能存在包含或者覆盖的状况 想要让所有球都联通 每个球有坐标x,y,z和半径 问还需要搭建最少多长的路 存储所有需要搭建的路 跑一遍最小生成树即可 注意原本就重叠的球距离0直接加入 代码如下: #include #include #include #include #include #de

ACMer' 1348

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

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

题目传送门:POJ - 2031 Building a Space Station 题目大意: 给你n个球体,输入n,接下俩输入n行中每行有四个数,分别代表球的球心坐标x,y,z 和球径r 。 可以在两球之间建立通道,现在要将所有球体联通,求最小代价。 分析: 可以知道任意两个球都可以建立通道,因此我们构建一张图,里面是所有球的所有通道,然后在跑prim就可以了 通道的大小是...

aochan2391的博客 113

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

poj2031 Building a Space Station 三维空间的最小生成树

题目链接: 2031 http://write.blog.csdn.net/postedit?ref=toolbar 题意: 在三维空间中有N个球形空间站,给出每个空间站的三维坐标x,y,z 和半径.其中空间站之间可能存在相交,包含,相离等情况。如果非相离则两空间站的距离为0。求连同所有空间站的最小生成树。 题解: 三维构图两空间站的

旧时光 1607

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

POJ 2031 Building a Space Station(最小生成树) http://poj.org/problem?id=2031 题意:        有n个三维的球体,对于每个球体给出了它的圆心坐标(x,y,z)以及它的半径r. 现在要你用最短的线使得所有球体属于同一个连通分量. 如果两个球体在初始的时候已经相切或重叠了,就认为它们已经连接上了,不用另外连线.否则需要连接长

code 1068

POJ 2031-Building a Space Station【最小生成树】

题目描述 You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task.  Th...

include_peng的博客 340

Building a Space Station POJ 2031 【最小生成树 prim】

http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, and are assigned a task in the c...

aoe41606的博客 127

POJ2031,Building a Space Station,最小生成树模版题

这道题其实就是最小生成树模版题,关键是要把题意读懂 解释一下题意(看题不用被题面所吓到,题目长不一定难!而且慢慢翻译文章也是一种乐趣! 空间里有很多球型的单位,它们的半径不一定都是一样的,要让它们都连通,(两个单位连通有三种情况:1)它们彼此挨着或者重叠;2)它们之间用一个通道直接连通(通道是连在球的表面);3)它们通过某个单位间接连通),然后是,它们是空间中的单位,如果A-B通过通道连通,C

北 593

poj 2031 Building a Space Station 【最小生成树 Prim】

Building a Space Station   Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5778   Accepted: 2874 Description You are a member of the space station engineering

longge33445的博客 665

POJ 2031 Building a Space Station 最小生成树

题意:有n个太空舱,给出每个太空舱球心坐标与球的半径,若两太空舱相邻或重叠则表示两太空舱相接,宇航员可以从A走到B当且仅当A、B直接或间接相接或者建造一条走廊将A、B直接或间接连接在一起,求将所有太空舱连接在一起的最短走廊长度 思路:若两太空舱直接相接则将其并到一个集合里,最小生成树边加一,否则将其距离作为权值放到边集里,注意给的坐标是球心坐标,故距离为两球心坐标距离减去两球半径,之后根据没连接的...

qq_42397248的博客 183

POJ 2031 Building a Space Station Kruskal最小生成树

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66965#problem/C 题意:大意是,给定一些空间站,把空间站当作球来处理,然后给定每个空间站的描述,球心坐标和半径,求两两连通的最小代价。两个求相连通的条件是: 1:相互相连或是重叠(直接相连) 2:通过第三者相连(间接相连) 思路:把直接相连的用并查集预处理就行

catch_catch的专栏 743

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

题目链接: POJ 2031 Building a Space Station 题意: 给定一些球的圆心与半径,如果两个球之间相交,则他们之间连通, 否则不连通,问还需要连多长的边使所有球都连通 分析:任意两个球,如果连通则边权为0, 否则边权为d-r1-r2,d是两球圆心坐标之间的距离,r1与r2分别为两个球的半径, 这样求一个最小生成树,将所有球连接起来 注意: 数据doubl

ramay7 479

poj2031 Building a Space Station 最小生成树,kruskal

题目链接:http://poj.org/problem?id=2031

basement_boy 689
上一篇: POJ Collecting Beepers (dfs 深度搜索)
下一篇: POJ 1856 Sea Battle(dfs)
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值