POJ 2031 Building a Space Station(Kruscal算法)

【代码超详解】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... 阅读详情

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

题意描述:
给出n个球的圆心坐标和半径,球与球之间是可以相互融合的,然后让你求出所有球互相连通的最短距离

解题思路:
先算出任意两个圆心之间的距离,然后减去它们两个的半径之和,如果半径之和大于这个距离就把这两个圆之间的距离赋值为0,不然就赋值为圆心距离减去半径之和,然后在跑一变最小生成树的模板就可以了。

#include<stdio.h>
#include<math.h>

struct node 
{
	double x,y,z,r,v;
}p[5055];

double f[105];

void qiucksort(int left,int right)
{
	int i,j;
	struct node t;
	if(left>right)
		return ;
		
	i=left;
	j=right;
	while(i!=j)
	{
		while(p[j].v>=p[left].v&&j>i)
			j--;
		while(p[i].v<=p[left].v&&j>i)
			i++;
		
		if(j>i)
		{
			t=p[i];
			p[i]=p[j];
			p[j]=t;
		}
	}
	t=p[left];
	p[left]=p[i];
	p[i]=t;
	
	qiucksort(left,i-1);
	qiucksort(i+1,right);
	
	return ;
}

int getf(int i)
{
	if(i==f[i])
		return i;
	
	f[i]=getf(f[i]);
	return f[i];
}

int merge(int x,int y)
{
	int t1,t2;
	t1=getf(x);
	t2=getf(y);
	if(t1!=t2)
	{
		f[t2]=t1;
		return 1;
	}
	return 0;
}

int main(void)
{
	int n,k,i,j;
	double e[105][105];
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
			break;
		for(i=1;i<=n;i++)
			scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z,&p[i].r);
		
		for(i=1;i<n;i++)
			for(j=i+1;j<=n;j++)
				e[i][j]=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y)+(p[i].z-p[j].z)*(p[i].z-p[j].z));
			
		for(i=1;i<n;i++)
		{
			for(j=i+1;j<=n;j++)
			{
				if(e[i][j]<p[i].r+p[j].r)
					e[i][j]=0;
				else
					e[i][j]=e[i][j]-p[i].r-p[j].r;
			}
		}
			
		k=1;
		for(i=1;i<n;i++)
		{
			for(j=i+1;j<=n;j++)
			{
				p[k].x=i;
				p[k].y=j;
				p[k].v=e[i][j];
				k++;
			}
		}
		qiucksort(1,k-1);
		
		for(i=1;i<=n;i++)
			f[i]=i;
		
		double sum=0;
		int count=0;
		for(i=1;i<k;i++)
		{
			if(merge(p[i].x,p[i].y)==1)
			{
				sum+=p[i].v;
				count++;
			 } 
			if(count==n-1)
				break;
		 } 
		 printf("%.3lf\n",sum);
	}
	return 0;
}
基于Dify与DeepSeek构建中医智能问答工作流:从知识库到精准处方 本文详细介绍了如何利用Dify与DeepSeek构建中医智能问答工作流,实现从知识库构建到精准处方的全流程。通过模型编排和知识检索技术,系统能够结合中医辨证思维与AI推理能力,为基层医师、学生和爱好者提供结构化诊疗建议。文章重点分享了知识库选材、症状标准化处理、智能检索与辨证推理等核心环节的实战经验。 阅读详情

相关推荐

Linux环境下离线升级GCC-9.4.0的完整实践指南

本文详细介绍了在Linux环境下离线升级GCC-9.4.0的完整实践指南,包括准备工作、依赖库安装、编译配置及常见问题解决。特别适用于企业内网或安全要求严格的服务器环境,帮助开发者高效完成GCC离线升级,提升编译性能并支持现代编程语言特性。

weixin_29288313的博客 670

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

解决Windows 10更新失败错误代码0x80070643

在Windows 10操作系统的更新过程中,用户时常会遇到各类错误代码,这些代码通常成为顺利完成更新的绊脚石。其中,错误代码0x80070643是一个较为常见的更新失败提示,它可能导致系统更新停滞不前。面对这一挑战,本文将详细介绍如何有效解决这一错误,帮助用户顺利完成Windows 10的更新。

nntxthml的博客 1827

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

终端效率神器:Tmux 安装与使用全指南

终端效率神器:Tmux 安装与使用全指南

身在被窝_心在编程 1358

el-transfer和el-tree进行结合搞一个树形穿梭框

由于业务需求需要在穿梭框里使用树形结构,但是本身element里并不支持,于是参考了别的大佬发的文章作为思路及后续自己新增了一些处理功能。

bbkhhvv的博客 6305

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

一文详解脉搏血氧测量原理与方法

本文摘要:血氧饱和度(SpO2)是评估人体氧合功能的重要指标,反映血红蛋白携氧能力。其测量基于光电容积描记法(PPG),利用红光(660nm)和红外光(940nm)通过组织时的吸收差异计算。脉搏波特征与心血管状态相关,动脉硬化会影响波形特征。Masimo公司的信号提取技术(SET)通过并行算法有效解决了传统血氧仪在运动、弱灌注情况下的测量误差问题,显著提升了临床监测的准确性和可靠性。该技术已成为血氧监测的金标准,在降低误报率、缩短响应时间方面具有显著优势。

m0_50146396的博客 2012

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. The space

温文尔雅 306

50、压电微泵与技术设备驱动的研究分析

本文研究了压电微泵在弹性微通道中的流体流动特性及技术设备驱动的振动行为。通过建立轴对称流固耦合模型,利用FreeFem++进行数值模拟,分析了不同数量压电致动器和频率对微泵流量的影响,发现多个致动器按特定时序压缩可产生净正向流量,并提出了稳态流量的近似解析公式。同时,针对技术设备驱动系统,探讨了周期性负载引起的强迫振动特性,明确了共振条件及影响因素,并比较了主动、被动和优化设计等振动控制策略。最后展望了实验验证、多物理场耦合与智能控制等未来研究方向,为相关设备的优化设计与高效运行提供了理论支持。

convnet3designer的博客 148

【Clickhouse】Clickhouse写入报错keeper server rejected the connection during the handshake

我的clickhouse集群时不时的报这个错误All connection tries failed while connecting to ZooKeeper. nodes: xxx 到时写入数据失败 用的ck自带的keeper。你需要仔细检查ZooKeeper集群的配置和状态,确保所有节点都正常运行,并且能够正确地选举领导者。检查ZooKeeper的ACL以及ClickHouse连接所使用的凭据,确保它们被正确配置和授权。版本不兼容:有时,不同版本的软件之间可能存在不兼容性,导致连接问题。

九师兄 371

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

Z2Min_ 368

解锁IPG Photonics SmartWeld系统,开启焊接新时代

SmartWeld 系统是 IPG Photonics 研发的一款先进的激光焊接解决方案,它隶属于自动化激光焊接系统类别 ,但却凭借一系列创新技术和独特设计,在众多同类产品中脱颖而出。该系统基于 IPG 在光纤激光技术领域的深厚积累,融合了先进的光束控制、智能监测和自动化控制等技术,旨在为工业制造提供高效、精准且稳定的焊接工艺。与传统激光焊接系统相比,SmartWeld 系统的独特之处首先体现在其光束控制技术上。它能够精确地调整激光束的能量分布、光斑大小和形状 ,以适应不同材料和焊接工艺的需求。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 1428

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

A - Building a Space Station Crawling in process...Crawling failedTime Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u SubmitStatus Description You are a member of ...

weixin_30892763的博客 321

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

SAP B1中文培訓教材_SDK

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

Building a Space Station (Kruskal)

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 sp

码农的一亩三分地 1538

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Building a Space Station--最小生成树

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7427   Accepted: 3601 Description You are a member of the space station engineerin

bless295的博客 407

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

首先求出所有的单间两两是否接触如果接触,说明他们之间的边长为0,不接触那么就是圆心的距离减去两个单间的半径。然后构图求最小生成树,采用Kruskal算法。

一蓑烟雨 1090

POJ2031-Building a Space Station

全解题报告索引目录 -> 【北大ACM – POJ试题分类】 转载请注明出处:http://exp-blog.com ------------------------------------------------------------------------- 大致题意: 就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通。如果两个球有重叠...

ζёСяêτ - 小優YoU 5181

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 the construction process of the station. You are expected to write a co...

旺崽的博客 3220

C - Building a Space Station POJ - 2031

C - Building a Space Station POJ - 2031 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 comput...

PinesL 306

POJ—Building a Space Station

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

weixin_34025051的博客 175

poj 2031 Building a Space Station(prim)

明明是最小生成树,分类却放在计算几何里面了。。。 每个小球是一个点,点之间的距离是球面之前的距离,如果两球覆盖,距离为0,然后prim#include <cstdio> #include <cstring> #include <cmath>const int MAXN = 110; const double INF = 1000.0; const double eps = 1e-10; struc

梦幻的蔷薇色 365

POJ 2031 Building a Space Station(最小生成树+简单几何)

题意: 在一个三位平面上有几个球体,然后输入数据是给你N个球的球心坐标,以及半径。科学家们想要实现各个球之间的接触,也就是有表面的接触。当然,两个球之间可能会有相交的地方( dis(a,b) &lt;= 0 ),那么这两个球是不用你新建道路来实现想通的。 思路: 求每个球之间球面的距离然后最小生成树就行了。水…… #include&lt;cstdio&gt; #include&lt;cs...

swineherd的博客 280

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

链接:  http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#problem/A Building a Space Station Time Limit: 1000MS   Memory Lim

free斩 2287
上一篇: HDU1102 Constructing Roads(prim)
下一篇: HDU 2594 Simpsons’ Hidden Talents(kmp)
鳳九~
博客等级 码龄8年 1384粉丝 215原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值