POJ - 2031 Building a Space Station 【PRIME】
题目链接
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】的更多相关文章
- 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【经典最小生成树】
链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- 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(prime )
这个题要交c++, 因为prime的返回值错了,改了一会 题目:http://poj.org/problem?id=2031 题意:就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能 ...
- 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 ...
随机推荐
- ThinkPHP 中M方法和D方法的具体区别
M方法和D方法的区别 ThinkPHP 中M方法和D方法都用于实例化一个模型类,M方法 用于高效实例化一个基础模型类,而 D方法 用于实例化一个用户定义模型类. 使用M方法 如果是如下情况,请考虑使用 ...
- const的限定
const对象一旦创建后,其值就不可以改变,所以const对象必须初始化.与非const的类型比较,主要区别在于const类型对象执行但是不改变其操作内容. 在默认状态下,const只在文件内有效.在 ...
- PGM图片格式与代码
这两天在搞神经网络,里面的一个人脸数据库的图片格式是PGM,事实上之前早就知道了这个图片格式,可是没去深究这个图片格式的数据究竟是什么安排的.搜索了下百度百科,发现介绍的真是简单,以下就自己来系统地整 ...
- PHP 变量定义及使用
php的变量前面必须有$符号,而且是解释型的弱类型语言,定义的时候不需要定义变量值的类型. $str="这是个变量"; 1.输出的时候可以用拼接字符串的方法 如:echo" ...
- The network connection was lost 文件下载错误提示
假设出现这种错误,可能是模拟器断网,重新启动下模拟器就能够:The network connection was lost
- Spring学习十二----------Bean的配置之@ImportResource和@Value
© 版权声明:本文为博主原创文章,转载请注明出处 @ImportResource -引入XML配置文件 @Value -从配置文件中获取值 实例 1.项目结构 2.pom.xml <projec ...
- spring-web中的StringHttpMessageConverter简介
spring的http请求内容转换,类似netty的handler转换.本文旨在通过分析StringHttpMessageConverter 来初步认识消息转换器HttpMessageConverte ...
- HDU 2473 Junk-Mail Filter 删点并查集
题目来源:pid=2473">HDU 2473 Junk-Mail Filter 题意:2中操作 M x, y 将x,y 合并到一个集合 S x 将x从所在的集合去掉 自己成为一个集合 ...
- java利用爬虫技术抓取(省、市(区号\邮编)、县)数据
近期项目须要用到 城市的地址信息,但从网上下载的xml数据没有几个是最新的地址信息.....数据太老,导致有些地区不全.所以才想到天气预报官网特定有最新最全的数据.贴出代码,希望能给有相同困惑的朋友. ...
- iOS中用UIWebView的loadHTMLString后图片和文字失调解决方法
iOS中用UIWebView的loadHTMLString后图片和文字失调,图片过大,超过屏幕,文字太小.或者图片太小.文字太大,总之就是不协调. 我们的需求是让图片的大小跟着屏幕的变化而变化.就是动 ...