题目链接

http://poj.org/problem?id=2031

题意

给出N个球形的 个体 如果 两个个体 相互接触 或者 包含 那么 这两个个体之间就能够互相通达 现在给出若干个这样的个体 要求 从一个个体 可以到达任意一个另外的个体 如果两个个体之间 本来是不能够相互通达的 那么可以在这两个个体之间 建一座桥梁 现在要求 满足 从任意一个个体就可以到达任意一个另外的个体 需要建设桥梁的最少花费

思路

因为两个球体之间,如果 两个球心的距离 <= 两个球体的半径之和 那么这两个球体 就是 可以通达的 边权为0 反之 边权 就是 球心距离减去半径之和

然后最小生成树 一下 就可以了 要加入 访问标记 因为 边权为0的 也是需要连通的

然后有一个奇怪的点

POJ 上面 我用G++ 提交 最后输出 要用 %.3f

然后用C++ 提交 就没有问题

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8; const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
const int MOD = 1e9 + 7; struct node
{
double x, y, z, r;
}q[maxn]; double G[maxn][maxn];
double lowcost[maxn];
int v[maxn]; double cover(node a, node b)
{
double dis = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z));
if (dis < a.r + b.r || fabs(dis - a.r - b.r) < eps)
return 0.0;
return dis - a.r - b.r;
} int n; int findMin()
{
double Min = INF * 1.0;
int flag = 0;
for (int i = 1; i <= n; i++)
{
if (v[i] == 0 && lowcost[i] < Min)
{
Min = lowcost[i];
flag = i;
}
}
return flag;
} double prime()
{
double ans = 0.0;
for (int i = 1; i <= n; i++)
lowcost[i] = G[1][i];
lowcost[1] = 0.0;
v[1] = 1;
for (int i = 2; i <= n; i++)
{
int k = findMin();
if (k)
{
ans += lowcost[k];
lowcost[k] = 0.0;
v[k] = 1;
for (int j = 1; j <= n; j++)
{
if (v[j] == 0 && G[k][j] < lowcost[j])
lowcost[j] = G[k][j];
}
}
}
return ans;
} int main()
{
while (scanf("%d", &n) && n)
{
CLR(v, 0);
for (int i = 1; i <= n; i++)
scanf("%lf%lf%lf%lf", &q[i].x, &q[i].y, &q[i].z, &q[i].r);
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
G[i][j] = G[j][i] = cover(q[i], q[j]);
}
printf("%.3lf\n", prime());
}
}

POJ - 2031 Building a Space Station 【PRIME】的更多相关文章

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

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

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

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

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

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

  4. poj 2031 Building a Space Station(prime )

    这个题要交c++, 因为prime的返回值错了,改了一会 题目:http://poj.org/problem?id=2031 题意:就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能 ...

  5. POJ 2031 Building a Space Station

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

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

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

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

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

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

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

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

随机推荐

  1. dede实战系统:更换成kindEditor编辑器

    最近由于项目需要,在搞dedeCMS,发现dede自带的ckEditor在word粘贴这方面做得不是很好,从word上面直接ctrl+V的内容跟wrod上面的表现样式相差很大,客户很不爽(因为是编辑的 ...

  2. 蒙特卡洛法MATLAB

    %%unifrnd函数的使用 %unifrnd函数可以创建随机的连续均匀分布的数组,一般式为R=unifrnd(A,B); %A和B是标量或者相同维数的行向量或者列向量.R=unifrnd(A,B,[ ...

  3. dede程序打开install安装时出现dir

    刚在网上找了个开源的dede程序,打开install安装时出现dir.解决方法例如以下: 1.删除install下的index.html和install_lock.txt(有的是要删除install. ...

  4. Interleaving String——是否由两个string交叉、DP

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  5. 【基础算法】排序-复杂排序之二(找出第K大的数)

    切割的思想是高速排序最精髓的地方.每一次切割出来的元素K一个排在第K位,所以利用这样的思想我们至少知道3点 1. 被切割出来的元素K最后一定排在第K位. 2. 在K左边的元素一定比K小或者相等. 3. ...

  6. UBUNTU : Destination Host Unreachable

    介绍我的系统的搭建的方式: WIN7 64 + VMWARE STATION,方式是进行桥接的方式.最近突然出现了问题,Ubuntu ping 外网或者 PING WIN 7 的时候,出现 Desti ...

  7. 用命令行执行ROBOT FRAMEWORK

    除了在ride中执行用例,我们也可以通过命令行的形式执行用例. 1.执行一整个项目 pybot+项目路径 2.执行某个测试集 pybot+测试集的路径 3.执行某个测试集里面的某个用例 pybot - ...

  8. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

  9. 手把手实现andriod应用增量升级

    近期研究了android应用增量升级的应用.当中用到了android NDK编程,先说下为什么要使用增量升级.当我们的应用达到一定大小的时候,比方眼下有30M.假设新版本号35M仅仅是添加了几个功能, ...

  10. javascript调试常用工具讲解

    .Console命令详解,让调试js代码变得更简单 2.<Firebug入门指南>