POJ2031 Building a Space Station【最小生成树】
题意:
就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通。如果两个球有重叠的部分则算为已连通,无需再搭桥。求搭建通路的最小边长总和是多少。
思路:
先处理空间点之间的距离,要注意的是两个球面相交的情况,相交的话距离是0。两球面距离是球心距离减去两个球的半径(边权 = AB球面距离 = A球心到B球心的距离 – A球半径 – B球半径),相交时距离是算出来是负值,要改为0。其实就是求连接所有球最小生成树的过程。
代码:
kruskal:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std; int n;
double need; struct Station //定义球的结构体
{
double x,y,z;
double r;
};
Station station[]; struct Vdge //构造生成树的点
{
int x,y;
double l;
}; bool cmp(Vdge a,Vdge b){
return a.l < b.l ;
} Vdge edge[];
int road[]; int find(int x)
{
while( x != road[x] )
x = road[x];
return x;
} bool judge(int x,int y){
int fx = find(x);
int fy = find(y);
if( fx==fy )
return false;
else
{
road[fx] = fy;
return true;
}
} double dis(int i,int j)
{
double distance;
double a = pow(station[i].x-station[j].x , );
double b = pow(station[i].y-station[j].y , );
double c = pow(station[i].z-station[j].z , );
distance = sqrt(a+b+c);
distance -= station[i].r + station[j].r;
if( distance <= ) distance = ;
return distance;
} void init()
{
for(int i= ; i<n ; i++)
scanf("%lf %lf %lf %lf",&station[i].x,&station[i].y,&station[i].z,&station[i].r);
for(int i= ; i<n ; i++)
road[i] = i;
need = ;
int k = ;
for(int i= ; i<n ; i++)
{
for(int j= ; j<n ; j++)
{
if( j>i )
{
edge[k].x = i;
edge[k].y = j;
edge[k].l = dis(i,j);
k++;
}
}
}
sort(edge,edge+n*(n-)/,cmp);
} void kruskal()
{
int i=,j=;
while( i<n- )
{
if( judge(edge[j].x,edge[j].y) )
need += edge[j].l , i++;
j++;
}
} int main()
{
while(cin>>n,n)
{
init();
kruskal();
printf("%.3f\n",need );
}
return ;
}
POJ2031 Building a Space Station【最小生成树】的更多相关文章
- POJ2031 Building a Space Station 2017-04-13 11:38 48人阅读 评论(0) 收藏
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8572 Accepte ...
- 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 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5173 Accepte ...
- 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 - 2031C - Building a Space Station最小生成树
You are a member of the space station engineering team, and are assigned a task in the construction ...
- POJ Building a Space Station 最小生成树
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15664 Accepted: 6865 Description You ...
- poj2031 Building a Space Station
这题目,用G++ WA,用C++ AC. 题目要求,现给出n个球,然后要使每两个球直接或者间接连通,可以在任意两球之间做管道(在表面),最后的要求是,如果使得都连通的话,管道最小长度是多少. 思路简单 ...
- POJ 2031 Building a Space Station 最小生成树模板
题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...
- POJ 2031 Building a Space Station (最小生成树)
Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...
随机推荐
- Delphi 2010 3513正式版破解
Delphi 2010 3513正式 一.下载ISO文件右键迅雷下载 二.安装完成后,先运行一下程序,程序会弹出叫你注册对话框,逐步点击Cancel---->OK----->No----& ...
- BZOJ3545&3551[ONTAK2010]Peaks——kruskal重构树+主席树+dfs序+树上倍增
题目描述 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只 ...
- python 随机数模块 -- random
一.概述 这个模块实现的伪随机数生成器. 对于整数,从区间选取.对于序列,随机元素. 在实线的,有功能来计算均匀分布,正态分布(高斯) ,对数正态分布,负指数,γ和β分布.对于生成的角度分布,冯·米塞 ...
- python -- 面向对象三大特性
1,继承 1,初识继承 什么是继承? ——继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类. 子类会“遗传”父类的属性,从而 ...
- 看完让你彻底理解 WebSocket 原理,附完整的实战代码(包含前端和后端)
1.前言 最近有同学问我有没有做过在线咨询功能.同时,公司也刚好让我接手一个 IM 项目.所以今天抽时间记录一下最近学习的内容.本文主要剖析了 WebSocket 的原理,以及附上一个完整的聊天室实战 ...
- codeforces 793B. Igor and his way to work
B. Igor and his way to work time limit per test 3 seconds memory limit per test 256 megabytes input ...
- jasperReport和Ireport
<!-- groovy --> <dependency> <groupId>org.codehaus.groovy</groupId> <arti ...
- Leetcode 237.删除链表中的节点 By Python
请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 4 -> 5 -> 1 - ...
- 自学Aruba7.2-Aruba安全认证-Portal认证(web页面配置)
点击返回:自学Aruba之路 自学Aruba7.2-Aruba安全认证-Portal认证(web页面配置) 步骤1 建立AP Group,命名为testportal-group 步骤2 将AP加入到 ...
- 自学Zabbix3.12-动作Action
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix3.12-动作Action介绍 1. 动作action 在配置好监控项和触发器之后 ...