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 ...
随机推荐
- Django 定义数据模型
如何定义数据模型: (1) 在 MVC 设计模式中,M 表示数据模型 ( Model ),负责业务对象与数据库的映射,我们可以通过应用的 models.py 来定义数据模型(2) Model 采用了 ...
- HTML 引用
关于 HTML 引用: (1) <q> 和 <blockquote> 用于实现长短不一的引用语(2) <q> 用于短的引用,<blockquote> 用 ...
- Excel导出到浏览器(个人备份)
/// <summary> /// Excel导出 /// </summary> /// <param name="dt"></pa ...
- 第六篇:Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)
需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...
- nodeJs学习过程之一个图片上传显示的例子
目标 1. 在浏览器地址栏输入“http://demos/start”,进入欢迎页面,页面有一个文件上传表单: 2. 选择一张图片并提交表单,文件被上传到"http://demos/uplo ...
- JS - url相关
今天在找获取当前网址除去参数的js方式,结果自己会的竟然只有window.location.href 查到的一篇博文: http://www.cnblogs.com/weiyuxinghuacun/a ...
- matplotlib包画基本的图
画直线图 1.最简单的用法: import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) #在(-1,1)范围内 ...
- Python pyQt4/PyQt5 学习笔记3(绝对对位,盒布局,网格布局)
本节研究布局管理的内容. (一)绝对对位 import sys from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__( ...
- SSL & TLS & STARTTLS
https://www.fastmail.com/help/technical/ssltlsstarttls.html SSL vs TLS vs STARTTLS There's often qui ...
- vsCode_1.27.2
User Settings: 一,当前行高亮显示: "editor.renderLineHighlight": "line", 二,如何呈现空白字符(一般选no ...