poj2031-Building a Space Station(最小生成树,kruskal,prime)

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

Building a Space Station
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5874 Accepted: 2914

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

最小生成树问题,大致意思是:空间站N隔球形的小室,有半径,和空间三围坐标,现在要连接通道,把所有的小室都能想通(直接或者间接),通道连在球的表面,如果两球有重叠部分,当然就不用连了!现在让球最少的路径和!


现在贡献prime和kruskal各一个!


prime:


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
int n,vis[110];
double x[110],y[110],z[110],r[110],low[110];
double map[110][110];
void prim()
{
	memset(low,INF,sizeof(low));
	int i,j;
	for(i=0;i<n;i++)
	{
		low[i]=map[0][i];
		vis[i]=0;
	}
	vis[0]=1;
	double sum=0;
	for(i=0;i<n;i++)
	{
		double mi=INF;
		int mark=-1;
		for(j=0;j<n;j++)
		{
			if(!vis[j]&&low[j]<mi)
			{
				mi=low[j];
				mark=j;
			}
		}
		if(mark==-1)
		break;
		sum+=mi;
		vis[mark]=1;
		for(j=1;j<n;j++)
		{
			if(!vis[j]&&low[j]>map[mark][j])
			low[j]=map[mark][j];
		}
	}
printf("%.3f\n",sum);
}
int main()
{
	int i,j;
	while(scanf("%d",&n),n)
	{
		memset(map,INF,sizeof(map));
		for(i=0;i<n;i++)
			scanf("%lf%lf%lf%lf",&x[i],&y[i],&z[i],&r[i]);
			int t=0;
		for(i=0;i<n;i++)
		{
			for(j=i;j<n;j++)
			{
				
				double q=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(q>(r[i]+r[j]))
				map[i][j]=map[j][i]=q-r[i]-r[j];
				else
				map[i][j]=map[j][i]=0;
			}
		}
		prim();
	}
return 0;
}


kruskal:



#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
int n,per[110];
double x[110],y[110],z[110],r[110];
struct node
{
	int s,e;
	double w;
}s[10000];
bool cmp(node x,node y)
{
	return x.w<y.w;
}
void init()
{
	for(int i=0;i<=n;i++)
	per[i]=i;
}

int find(int x)
{
	int t=x,j;
	while(t!=per[t])
	t=per[t];
	while(x!=t)
	{	
		j=per[x];
		per[x]=t;
		x=j;
	}
	return t;
}
bool join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		per[fx]=fy;
		return true;
	}
	return false;
}
int main()
{
	int i,j;
	while(scanf("%d",&n),n)
	{
		init();
		for(i=0;i<n;i++)
			scanf("%lf%lf%lf%lf",&x[i],&y[i],&z[i],&r[i]);
			int t=0;
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				s[t].s=i;
				s[t].e=j;
				double q=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(q>(r[i]+r[j]))
				s[t++].w=q-r[i]-r[j];
				else
				join(i,j);
			}
		}
		sort(s,s+t,cmp);
		double sum=0;
		for(i=0;i<t;i++)
		{
			if(join(s[i].s,s[i].e))
			sum+=s[i].w;
		}
		printf("%.3f\n",sum);
	}
return 0;
}




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

相关推荐

树莓派 SPI操作

扩展板配备一款0.96inch,128864分辨率的oled显示屏,通过SPI控制,先上显示效果图。由于程序过长,在这里我就不把代码全部贴出来了,只是简单讲解一下spi的操作函数。管脚如下如所有,左边方框的管脚分别为MOSI MISO SCLK,左边CE0,CE1为两个片选管脚,分别对应上图中的spidev0.0,spidev0.1两个设备。下载spidev库,复制到树莓派,并行运行如下命令解压安装。spi设备,此处设备为/dev/spi-decv0.0。在/dev路径下面,我们可以发现两个spi设备。

weixin_46529898的博客 914

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

想学嵌入式但没开发板?试试这个免费的STM32虚拟仿真平台(含LVGL界面教程)

本文介绍如何利用免费的STM32虚拟仿真平台学习嵌入式开发,特别适合预算有限或零硬件基础的学习者。通过详细的LVGL界面开发教程,展示虚拟仿真在嵌入式学习中的高效性和实用性,涵盖环境搭建、代码实现到实机迁移的全流程,助力快速掌握嵌入式开发核心技能。

weixin_29266647的博客 306

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

保姆级教程:用Zemax 18.9复现单模光纤耦合仿真(附康宁SMF-28e参数)

本文提供了一份详细的Zemax 18.9单模光纤耦合仿真教程,以康宁SMF-28e为例,涵盖从软件设置、材料参数输入到系统构建和优化的全流程。通过光束耦合分析和物理光学传播(POP)技术,帮助用户准确模拟光纤耦合效率,并解决常见问题,适用于光学设计初学者和专业人士。

weixin_27330481的博客 313

手把手教你理解特斯拉Model 3的域控制器架构:RBCM如何成为“多面手”?

本文深入解析特斯拉Model 3的域控制器架构,重点介绍右车身域控制器(RBCM)如何整合车身电子系统功能。RBCM作为多任务处理中枢,管理空调系统、尾部电气设备和超声波雷达等关键功能,展现了特斯拉在汽车电子架构上的创新设计。文章详细探讨了RBCM的硬件架构、功能版图及信号网络,揭示其如何通过高性能MCU和智能开关技术实现功能集中与效率提升。

weixin_30919429的博客 383

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

SAP FI 提示没有为会计年度0 定义版本2025 GP626

SAP 错误 GP626 是一个常见的系统消息,通常表示某个会计年度的版本(如版本 0)未被定义或未激活。:在执行某些事务(如创建采购订单、过账凭证或维护成本中心)时,系统会检查相关版本是否已定义。的“Settings for Each Fiscal Year”界面中,选择一个已激活的会计年度。的“Settings for Each Fiscal Year”界面中,找到未激活的会计年度。:在控制范围(Controlling Area)中,未为指定的会计年度定义版本 0。如果没有,添加或复制现有年度的设置。

GAVIN 2489

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

【入门篇】第五章 第一次与 AI Agent 交互

本文介绍了OpenClaw中最基本的用户与AI Agent交互方式,剖析了Agent和Gateway两个核心组件的作用,通过简单示例展示了从用户请求到结果返回的完整执行流程,说明了最小交互过程对验证系统可用性的重要意义,为后续学习复杂任务执行机制奠定基础。

lanyv的博客 512

【GameFramework框架】三、快速启动

这篇文章就带领大家如何快速启动GameFramework框架。

优快云博客专家,Unity3D高级软件工程师,《Unity 3D从入门到实战》作者。 4427

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

SAP Event Mesh 简介

Get Started with Messaging for Decoupled Communication Step 1: What is the SAP Event Mesh service? SAP Event Mesh 是一种完全托管的服务,用于连接应用程序、服务和系统,以便它们可以通过消息和事件相互交互。 它通过引入中央消息代理取代了传统的点对点通信。 虽然点对点通信适用于在有限数量的发送方和接收方之间共享数据,但可扩展性很快就会成为一个问题。 SAP Event Mesh 解决了这个问题,并确保

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1242

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

POJ 2031

 #include #include #include #include #include using namespace std; #define esp 10E-9 int k,n; int p[105]; struct Edge { int u,v; double w; }edge[105*105]; struct Cor { double x,y,z,r;

mowenwen_的博客 438

Statistics and Machine Learning Toolbox

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

POJ 2031 Building a Space Station

题目链接:https://vjudge.net/problem/POJ-2031 题目大意   给定 N 个空间站,每个空间站都有自己的三维坐标和半径,任意两个空间站都互相可达,如果两个空间接触或者有重叠,则记为无走廊可达;反之就是有走廊可达,问为了使空间站两两可达,最短需要修建的走廊长度为多少。 分析   同 POJ 1789。 代码如下 1 #inclu...

weixin_30897079的博客 181

[Kruskal] POJ 2031

题意: 给定一些球的圆心与半径,如果两个球之间相交,则他们之间连通, 否则不连通,问还需要连多长的边使所有球都连通

Aria461863631的博客 342

poj2031--Building a Space Station

题意:给你n个球 坐标 半径。球若相互覆盖或接触就算相连 让你求出最小的长度使得从任意一球出发能到达任意球; 思路:最小生成树 代码用g++交WA 用c++就A 无语。。。 #include #include #include #include #include #include #include #include #define N 110 #def

bigsungod的专栏 701

poj2031

题意:给你N个细胞之类的东西,简单理解为球,这是立体的所以球心坐标是x,y,z这样的,然后给你一个球的半径,然后把这些球连接起来,如果两个球之间不想交,那么修建一条道路连起来,如果相交,那么不需要修路,求修路的最短是多少,其实就是最小生成树 思想:不想交距离等于两个点之间距离减去两个球半径,相交距离为0; 这个题除了一晚上bug原来是自己的distance和库函数重名了,改成dist调用就好了

tydfhihfhgjkj的博客 523

poj 2031. Building a Space Station

题目概述 解题思路 根据题意,这道题是要求给定的N个三维球体的最小生成树。这道题的数据特点在于: 点不是特别多(100以内) 但是每两个点之间都需要计算一下距离(E~V^2) 这就是说,我们采用邻接矩阵存储图是更好的策略。 另外,考虑到边的数目非常多,我们采用类似于Prim算法的思想来求解,即维护最小生成树的子树,每次添加未使用的节点到该子树的最小的边。 方法性能 方法的时间复杂度为O(): 示例代码 ...

georgeandgeorge的博客 187

POJ - 2031 Building a Space Station(Prim)

POJ - 2031 Building a Space Station #include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; const int N = 110; double x[N], y[N], z[N], r[N]; double g[N][N], d[N]; bool st[N]; int main() { in

00后的博客 463

POJ - 2031 Building a Space Station (prim)

题意:给出球形空间站数目N,以及各个空间站的三维坐标x,y,z 以及 半径r ,求将所有空间站连接的最小cost (cost就等于空间站之间的距离) 如果接触,包含,或者相交则不需要搭建桥 思路:还是一道最小生成树的题目,我们先记录每个空间站的信息,然后将所有空间站两两相连接,如果 如果接触,包含,或者相交 我们就把cost 置为1 ,否则就用距离半径之和。 把每个空间站用一个标号表...

154

POJ2031

题目大意 给你n个空间直角坐标作为球心,告诉你球的半径,在球表面之间修路,问路最少修多长能够使得球相互可达 练习一下简单的最小生成树 首先需要解决几个问题 建图 如何判断球之间不需要修路 一共只有不到100个输入,所以可以两两之间计算路径长,用并查集判断球之间不需要修路,之后就是正常的最小生成树了 #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> usin

ComingLiu的博客 216

poj-2031

C - Building a Space Station  题意:在一个三维空间里建造球形的小室,若小室相邻或部分重叠,或者间接有走廊间接相连,则两个小室相连通,给出n个小室,下面n行每行分别是n个小室的球形空间坐标,和球形小室的半径长度。找出让所有球形小室联通所需要建造长廊的最短距离。 思路:相邻两球的距离用球心坐标,和两个球心半径比较。二维数组邻接矩阵存图,两个小室之间所需要长廊的距离为两...

pallas_的博客 381

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
上一篇: hdu3665-Seaside(SPFA,dijkstra,floyd)
下一篇: 参观城市(dfs,改编spfa)
Eric_keke
博客等级 码龄12年 37粉丝 221原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值