Building a Space Station---poj2031(最小生成树)
题目链接:http://poj.org/problem?id=2031
n个球型的cell,如果任意两个球表面没有接触或者没有包含关系,就选择最近的表面建立通道;
所以用maps[i][j]表示i,j之间的距离;最后求所有连接的和的最小值;
注意:
poj上:对于双精度输出,G++上面要用%f,C++则用%lf,否则WA
#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<math.h>
#define N 110
#define INF 0xfffffff using namespace std; int vis[N], n;
double maps[N][N], dist[N]; struct node
{
double x, y, z, r;
}a[N*N]; void Init()
{
int i;
memset(a, ,sizeof(a));
memset(vis, ,sizeof(vis));
for(i=; i<=n; i++)
{
dist[i] = INF;
for(int j=; j<=n; j++)
{
maps[i][j] = INF;
}
maps[i][i] = ;
}
} double Prim(int start)
{
double ans = ;
vis[start] = ;
for(int i=; i<=n; i++)
dist[i] = maps[start][i];
for(int i=; i<=n; i++)
{
double Min = INF;
int index = -;
for(int j=; j<=n; j++)
{
if(vis[j] == && Min > dist[j])
{
Min = dist[j];
index = j;
}
}
if(index == -)break;
vis[index] = ;
ans += Min;
for(int j=; j<=n; j++)
{
if(vis[j] == && dist[j] > maps[index][j])
dist[j] = maps[index][j];
}
}
return ans;
} int main()
{
int i, j;
double ans, x, y, z, d;
while(scanf("%d", &n), n)
{
Init();
for(i=; i<=n; i++)
{
scanf("%lf%lf%lf%lf", &a[i].x, &a[i].y, &a[i].z, &a[i].r);
}
for(i=; i<=n; i++)
{
for(j=i+; j<=n; j++)
{
x = a[i].x - a[j].x;
y = a[i].y - a[j].y;
z = a[i].z - a[j].z;
d = sqrt( x*x + y*y + z*z );
if(d > a[i].r + a[j].r)
{
maps[i][j] = maps[j][i] = d - a[i].r - a[j].r;
}
else
{
maps[i][j] = maps[j][i] = ;
}
}
}
ans = Prim();
printf("%.3lf\n", ans);
}
return ;
}
Building a Space Station---poj2031(最小生成树)的更多相关文章
- 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【最小生成树prime】【模板题】
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5699 Accepte ...
- 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 ...
- POJ2031 Building a Space Station【最小生成树】
题意: 就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则算为已连通,无需再搭桥.求搭建通路的最小边长总和是多少. 思路: 先处理空间点之间的距离 ...
- poj 2031 Building a Space Station(最小生成树,三维,基础)
只是坐标变成三维得了,而且要减去两边的半径而已 题目 //最小生成树,只是变成三维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> ...
- POJ2031 Building a Space Station 2017-04-13 11:38 48人阅读 评论(0) 收藏
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8572 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 最小生成树
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6083 Accepte ...
- POJ 2031 Building a Space Station (最小生成树)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5173 Accepte ...
随机推荐
- 执行RF测试只生成output.xml文件,不生成log和report文件
命令格式: -l --log file HTML log file. Can be disabled by giving a special value `NONE`. Default: log.ht ...
- 解决layui下拉选择框只能选择不能手动输入文字
审查元素可以看到,layui的select下拉框是用input和div模拟出来的,所以,如下例子,我的解决方法是:$('.mySelect').find('input').removeAttr(&qu ...
- 32位win7+vs2008编译mysql 5.6.22源码并安装
以下这部分安装说明是来自http://www.2cto.com/database/201407/316681.html的win7+vs2010源码编译mysql,文章最后会说明用vs2008编译遇见的 ...
- JS使用中碰到的一些问题
settimeout: 1.setTimeout(function () {//这个则会在1秒后进行弹出1 alert(1); }, 1000); 2.setTimeout(alert(1), 100 ...
- thinkphp3.2 实现二级导航和高亮显示
一.控制器代码如下: public function index(){ //高亮显示 $action = CONTROLLER_NAME."/".ACTION_NAME; $thi ...
- Android M App休眠 (adb shell dumpsys usagestats)
App休眠 在 Marshmallow 系统,Google 宣布了一个新的功能叫 App 休眠.App 休眠会阻止那些不 常用的 App(几天没有用过的 App)连接网络或者是运行任何程序直至设备充电 ...
- how-to-build-c-static-libraries-boost
http://tungchingkai.blogspot.jp/2016/11/how-to-build-c-static-libraries-boost.html How to build C++ ...
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description 姊妹篇:http://www. ...
- win10 与linux mint双系统 只能进入mint而无法进入windows的解决方案
新购买了一块ssd,和以前的hdd硬盘一起装双系统:win10和mint ssd:win10 sdb1 sdb2 sdb3 sda2 hdd: mint sda1 ...
- CF 166E Tetrahedron
E. Tetrahedron time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...