Prim POJ 2031 Building a Space Station
题意:给出n个三维空间的球体,球体是以圆心坐标+半径来表示的,要求在球面上建桥使所有的球联通,求联通所建桥的最小长度。
分析:若两点距离大于两半径和的长度,那么距离就是两点距离 - 半径和,否则为0,Prim写错了,算法没有完全理解
/************************************************
* Author :Running_Time
* Created Time :2015/10/25 12:00:48
* File Name :POJ_2031.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e2 + 10;
const int E = N * N;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10; bool vis[N];
double d[N];
int head[N];
int n, m, e;
int dcmp(double x) { //三态函数,减少精度问题
if (fabs (x) < EPS) return 0;
else return x < 0 ? -1 : 1;
}
struct Point {
double x, y, z, a;
Point () {}
Point (double x, double y, double z, double a) : x (x), y (y), z (z), a (a) {}
Point operator - (const Point &r) const { //向量减法
return Point (x - r.x, y - r.y, z - r.z, 0);
}
};
typedef Point Vector; //向量的定义
Point read_point(void) { //点的读入
double x, y, z, r;
scanf ("%lf%lf%lf%lf", &x, &y, &z, &r);
return Point (x, y, z, r);
}
double dot(Vector A, Vector B) { //向量点积
return A.x * B.x + A.y * B.y + A.z * B.z;
}
double length(Vector A) { //向量长度,点积
return sqrt (dot (A, A));
} struct Edge {
int v, nex;
double w;
Edge () {}
Edge (int v, double w, int nex) : v (v), w (w), nex (nex) {}
bool operator < (const Edge &r) const {
return w > r.w;
}
}edge[E]; void init(void) {
memset (head, -1, sizeof (head));
e = 0;
} void add_edge(int u, int v, double w) {
edge[e] = Edge (v, w, head[u]);
head[u] = e++;
} double Prim(int s) {
memset (vis, false, sizeof (vis));
for (int i=0; i<n; ++i) d[i] = 1e9;
priority_queue<Edge> Q;
for (int i=head[s]; ~i; i=edge[i].nex) {
int v = edge[i].v; double w = edge[i].w;
if (d[v] > w) {
d[v] = w; Q.push (Edge (v, d[v], 0));
}
}
vis[s] = true; d[s] = 0;
double ret = 0;
while (!Q.empty ()) {
int u = Q.top ().v; Q.pop ();
if (vis[u]) continue;
vis[u] = true; ret += d[u];
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v; double w = edge[i].w;
if (!vis[v] && d[v] > w) {
d[v] = w; Q.push (Edge (v, d[v], 0));
}
}
}
return ret;
} Point p[N];
int main(void) {
while (scanf ("%d", &n) == 1) {
if (!n) break;
for (int i=0; i<n; ++i) {
p[i] = read_point ();
}
init ();
for (int i=0; i<n; ++i) {
for (int j=i+1; j<n; ++j) {
double dis = length (p[i] - p[j]);
double len = p[i].a + p[j].a;
if (dcmp (dis - len) <= 0) {
add_edge (i, j, 0);
add_edge (j, i, 0);
}
else {
add_edge (i, j, dis - len);
add_edge (j, i, dis - len);
}
}
}
printf ("%.3f\n", Prim (0));
} return 0;
}
Prim POJ 2031 Building a Space Station的更多相关文章
- 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
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- 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 (最小生成树)
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 (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 (计算几何+最小生成树)
题目: 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 ...
随机推荐
- 0day漏洞是什么意思啊?
在报纸上看到xx又爆出0day漏洞是什么意思? day漏洞,是已经发现但是官方还没发布补丁的漏洞. 信息安全意义上的0Day是指在安全补丁发布前而被了解和掌握的漏洞信息. http://baike.b ...
- [Effective JavaScript 笔记]第27条:使用闭包而不是字符串来封装代码
函数是一种将代码作为数据结构存储的便利方式,代码之后可以被执行.这使得富有表现力的高阶函数抽象如map和forEach成为可能.它也是js异步I/O方法的核心.与此同时,也可以将代码表示为字符串的形式 ...
- 华为2013校招之哈工大威海 上机试题之一:报数问题:设有N 个人围坐一圈并按顺时针方向从1 到N 编号,从第S个人开始进行1 到M报数,报 数到第 M个人时,此人出圈,再从他的下一个人重新开始1 到 M的报数,如此进行下去直 到所有的人都出圈为止。现要打印出出圈次序。
1. 报数游戏 问题描述: 设有N 个人围坐一圈并按顺时针方向从1 到N 编号,从第S个人开始进行1 到M报数,报 数到第 M个人时,此人出圈,再从他的下一个人重新开始1 到 M的报数,如此进行下去 ...
- java.sql.date与java.util.date区别以及数据库中插入带时分秒的时间
java.sql.Date,java.sql.Time和java.sql.Timestamp三个都是java.util.Date的子类(包装类). java.sql.Date是java.util.Da ...
- [官方教程] [ES4封装教程]2.使用 Easy Sysprep v4 封装 Windows XP
(一)备份当前操作系统封装的第一步,其实是备份当前安装好的操作系统.避免我们在之后的步骤中出现问题,以至于还要重新安装操作系统,浪费时间精力.系统备份想必大家都会.对于WinXP而言,建议使用Ghos ...
- Linux 高可用开源方案 Keepalived VS Heartbeat对比
1)Keepalived使用更简单:从安装.配置.使用.维护等角度上对比,Keepalived都比Heartbeat要简单得多,尤其是Heartbeat2.1.4后拆分成3个子项目,安装.配置.使用都 ...
- 以Python角度学习Javascript(二)之DOM
HTML DOM 定义了访问和操作 HTML 文档的标准方法. DOM 将 HTML 文档表达为树结构. 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文 ...
- qq空间答案
2073693795 懂得人自然会懂
- Java基础算法集50题
最近因为要准备实习,还有一个蓝桥杯的编程比赛,所以准备加强一下算法这块,然后百度了一下java基础算法,看到的都是那50套题,那就花了差不多三个晚自习的时间吧,大体看了一遍,做了其中的27道题,有一些 ...
- "int?" 是什么类型?和"int"有何区别
int?:表示可空类型,就是一种特殊的值类型,它的值可以为null用于给变量设初值得时候,给变量(int类型)赋值为null,而不是0int??:用于判断并赋值,先判断当前变量是否为null,如果是就 ...