POJ-2031 Building a Space Station---MST + 空间距离
题目链接:
https://vjudge.net/problem/POJ-2031
题目大意:
就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通。如果两个球有重叠的部分则算为已连通,无需再搭桥。求搭建通路的最小费用(费用就是边权,就是两个球面之间的距离)。
思路:
MST模板+空间距离计算
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
typedef long long ll;
const int maxn = 2e2 + ;
const int INF = << ;
int dir[][] = {,,,,-,,,-};
int T, n, m, x;
struct node
{
double x, y, z, r;
node(){}
node(double x, double y, double z, double r):x(x), y(y), z(z), r(r){}
};
node a[maxn];
double Map[maxn][maxn];//存图
double lowcost[maxn], mst[maxn];
void prim(int u)//最小生成树起点
{
double sum_mst = ;//最小生成树权值
for(int i = ; i <= n; i++)//初始化两个数组
{
lowcost[i] = Map[u][i];
mst[i] = u;
}
mst[u] = -;//设置成-1表示已经加入mst
for(int i = ; i <= n; i++)
{
double minn = INF;
int v = -;
//在lowcost数组中寻找未加入mst的最小值
for(int j = ; j <= n; j++)
{
if(mst[j] != - && lowcost[j] < minn)
{
v = j;
minn = lowcost[j];
}
}
if(v != -)//v=-1表示未找到最小的边,
{//v表示当前距离mst最短的点
//printf("%d %d %d\n", mst[v], v, lowcost[v]);//输出路径
mst[v] = -;
sum_mst += lowcost[v];
for(int j = ; j <= n; j++)//更新最短边
{
if(mst[j] != - && lowcost[j] > Map[v][j])
{
lowcost[j] = Map[v][j];
mst[j] = v;
}
}
}
}
//printf("weight of mst is %d\n", sum_mst);
printf("%.3f\n", sum_mst);
}
double f(int i, int j)
{
double l;
l = sqrt((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y) + (a[i].z - a[j].z) * (a[i].z - a[j].z));
l = l - a[i].r - a[j].r;
if(l <= )return ;
else return l;
}
int main()
{
while(cin >> n && n)
{
for(int i = ; i <= n; i++)cin >> a[i].x >> a[i].y >> a[i].z >> a[i].r;
for(int i = ; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
Map[i][j] = Map[j][i] = f(i, j);
}
}
prim();
}
return ;
}
POJ-2031 Building a Space Station---MST + 空间距离的更多相关文章
- 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【最小生成树prime】【模板题】
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5699 Accepte ...
- POJ 2031 Building a Space Station
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- 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 (最小生成树)
Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...
- POJ - 2031 Building a Space Station 三维球点生成树Kruskal
Building a Space Station You are a member of the space station engineering team, and are assigned a ...
- POJ 2031 Building a Space Station (计算几何+最小生成树)
题目: Description You are a member of the space station engineering team, and are assigned a task in t ...
- POJ 2031 Building a Space Station【最小生成树+简单计算几何】
You are a member of the space station engineering team, and are assigned a task in the construction ...
- POJ 2031 Building a Space Station (prim裸题)
Description You are a member of the space station engineering team, and are assigned a task in the c ...
- poj 2031 Building a Space Station(prime )
这个题要交c++, 因为prime的返回值错了,改了一会 题目:http://poj.org/problem?id=2031 题意:就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能 ...
随机推荐
- 剑指Offer-删除链表中重复的结点
package LinkedList; /** * 删除链表中重复的结点 * 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. * 例如,链表1-> ...
- express+mysqle
var mysql=require('mysql'); var connection=mysql.createConnection({ host:'',//地址 port:'',//端口号 user: ...
- 桶排序/基数排序(Radix Sort)
说基数排序之前,我们先说桶排序: 基本思想:是将阵列分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递回方式继续使用桶排序进行排序).桶排序是鸽巢排序的一种归纳结果.当要被排序 ...
- .NET Core快速入门教程 3、我的第一个.NET Core App (CentOS篇)
一.前言 本篇开发环境?1.操作系统:CentOS7(因为ken比较偏爱CentOS7)2.SDK版本:.NET Core 2.0 Preview 你可能需要的前置知识1.了解如何通过Hyper-V安 ...
- [poj3687]Labeling Balls_拓扑排序
Labeling Balls poj-3687 题目大意:给出一些球之间的大小关系,求在满足这样的关系下,编号小的尽量比编号大的球的方案. 注释:1<=N(球的个数)<=200,1< ...
- [poj3252]Round Numbers_数位dp
Round Numbers poj3252 题目大意:求一段区间内Round Numbers的个数. 注释:如果一个数的二进制表示中0的个数不少于1的个数,我们就说这个数是Round Number.给 ...
- Java创建线程的三种方式
一.继承Thread类创建线程类 (1)定义Thread类的子类,并重写该类的run方法,该run方法的方法体就代表了线程要完成的任务.因此把run()方法称为执行体. (2)创建Thread子类的实 ...
- 常用排序算法的Java实现与分析
由于需要分析算法的最好时间复杂度和最坏时间复杂度,因此这篇文章中写的排序都是从小到大的升序排序. 带排序的数组为arr,arr的长度为N.时间复杂度使用TC表示,额外空间复杂度使用SC表示. 好多代码 ...
- SpagoBi开发示例——员工离职人数统计
1.开发工具:SpagoBIStudio_5.1,操作界面和使用方法和eclipse没差 安装参考:http://www.cnblogs.com/starlet/p/4778334.html 2. ...
- [福大软工] W班 软工实践原型设计—成绩公布
作业地址 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/909 作业要求 详见作业地址 存在问题 1. ...