题目传送门

题意:给出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的更多相关文章

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

    链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  2. POJ 2031 Building a Space Station

    3维空间中的最小生成树....好久没碰关于图的东西了.....              Building a Space Station Time Limit: 1000MS   Memory Li ...

  3. poj 2031 Building a Space Station【最小生成树prime】【模板题】

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepte ...

  4. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5173   Accepte ...

  5. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...

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

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

  8. POJ 2031 Building a Space Station (计算几何+最小生成树)

    题目: Description You are a member of the space station engineering team, and are assigned a task in t ...

  9. POJ 2031 Building a Space Station【最小生成树+简单计算几何】

    You are a member of the space station engineering team, and are assigned a task in the construction ...

随机推荐

  1. acdream.LCM Challenge(数学推导)

     LCM Challenge Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit ...

  2. XSS代码触发条件,插入XSS代码的常用方法

    1.脚本插入 (1)插入javascript和vbscript正常字符. 例1:<img src=”javascript:alert(/xss/)”> 例2:<table backg ...

  3. Windbg对过滤驱动DriverEntry函数下断点技巧

    方法1: 1> 先用DeviceTree.exe查看指定的过滤驱动的Load Address(加载地址) 2> 再用LordPE.EXE查看指定过滤驱动文件的入口点地址 3> 计算过 ...

  4. 淘宝(阿里百川)手机客户端开发日记第九篇 Looper详解

    public final class Looper: 官方的API: Class used to run a message loop for a thread. Threads by default ...

  5. git寻根——^和~的区别

    一. 引子 在git操作中,我们可以使用checkout命令检出某个状态下文件,也可以使用reset命令重置到某个状态,这里所说的“某个状态”其实对应的就是一个提交(commit). 我们可以把一个g ...

  6. MYSQL随机抽取查询 MySQL Order By Rand()效率问题

    MYSQL随机抽取查询:MySQL Order By Rand()效率问题一直是开发人员的常见问题,俺们不是DBA,没有那么牛B,所只能慢慢研究咯,最近由于项目问题,需要大概研究了一下MYSQL的随机 ...

  7. jquery博客收集的IE6中CSS常见BUG全集及解决方案

    今天的样式调的纠结,一会这边一会那么把jquery博客折腾的头大,浏览器兼容性.晚上闲着收集一些常见IE6中的BUG 3像素问题及解决办法 当使用float浮动容器后,在IE6下会产生3px的空隙,有 ...

  8. 在VMware的虚拟机平台上如何进行网络设置

    1.本文构建的是这样一个网络,有两台winXP系统的PC,处于同一局域网内,PC里 都装有VMware虚拟机,虚拟机上跑的是Redhat Linux 9,我们想要在winXP系统下访问本机的虚拟机li ...

  9. Mybatis 动态sql标签

    1.动态SQL片段 通过SQL片段达到代码复用         <!-- 动态条件分页查询 -->          <sql id="sql_count"> ...

  10. C#静态static的用法

    一.静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 new 关键字创建静态类类型的变量.在声明一个类时使用static关键字,具有两个方面的意义:首先,它防止程序员写代码来 ...