1000 - Spoon Devil's 3-D Matrix

Time Limit:1s Memory Limit:32MByte

Submissions:208Solved:65

DESCRIPTION

Spoon Devil build a 3-D matrix, and he(or she) wants to know if he builds some bases what's the shortest distance to connect all of them.

INPUT
There are multiple test cases. The first line of input contains an integer
T

, indicating the number of test cases. For each test case:The first line contains one integer
n (0<n<50),
indicating the number of all points. Then the next
n
lines, each lines contains three numbers
xi,yi,zi
indicating the position of i

-th point.
OUTPUT
For each test case, output a line, which should accurately rounded to two decimals.
SAMPLE INPUT
221 1 02 2 031 2 30 0 01 1 1
SAMPLE OUTPUT
1.413.97

思路:

三维点的MST(Krusal)

#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int MAXN=55;
const int MAXM=1200;
int pre[MAXN];
struct node
{
double x,y,z;
node()
{
x=y=z=0;
}
}Node[MAXN];
struct edge
{
int s,e;
double d;
edge()
{
s=e=d=0;
}
}Edge[MAXM];
bool cmp(edge a, edge b)
{
return a.d<b.d;
}
int father(int x)
{
if(pre[x]==x)
return x;
else
{
pre[x]=father(pre[x]);
return pre[x];
}
}
double krusal(int n)
{
double cost=0;
for(int i=0;i<MAXN;i++)pre[i]=i;
int cnt=0;
int index=0;
while(cnt<n-1)
{
int ps=father(Edge[index].s);
int pe=father(Edge[index].e);
if(ps!=pe)
{
pre[ps]=pe;
cost+=Edge[index].d;
cnt++;
}
index++;
}
return cost;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lf%lf%lf",&Node[i].x,&Node[i].y,&Node[i].z);
}
int cnt=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
Edge[cnt].s=i;Edge[cnt].e=j;
Edge[cnt].d=sqrt(pow(fabs(Node[i].x-Node[j].x),2.0)+pow(fabs(Node[i].y-Node[j].y),2.0)+pow(fabs(Node[i].z-Node[j].z),2.0));
cnt++;
}
}
sort(Edge,Edge+cnt,cmp);
printf("%.2lf\n",krusal(n));
}
return 0;
}

Lonlife 1000 - Spoon Devil's 3-D Matrix的更多相关文章

  1. Spiral Matrix(LintCode)

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  2. 《转》循环神经网络(RNN, Recurrent Neural Networks)学习笔记:基础理论

    转自 http://blog.csdn.net/xingzhedai/article/details/53144126 更多参考:http://blog.csdn.net/mafeiyu80/arti ...

  3. Strassen algorithm(O(n^lg7))

    Let A, B be two square matrices over a ring R. We want to calculate the matrix product C as {\displa ...

  4. (数据科学学习手札16)K-modes聚类法的简介&Python与R的实现

    我们之前经常提起的K-means算法虽然比较经典,但其有不少的局限,为了改变K-means对异常值的敏感情况,我们介绍了K-medoids算法,而为了解决K-means只能处理数值型数据的情况,本篇便 ...

  5. <Sicily>Tiling a Grid With Dominoes

    一.题目描述 We wish to tile a grid 4 units high and N units long with rectangles (dominoes) 2 units by on ...

  6. 【算法导论】--分治策略Strassen算法(运用下标运算)【c++】

    由于偷懒不想用泛型,所以直接用了整型来写了一份 ①首先你得有一个矩阵的class Matrix ②Matrix为了方便用下标进行运算, Matrix的结构如图:(我知道我的字丑...) Matrix. ...

  7. 循环神经网络RNN

    转自 http://blog.csdn.net/xingzhedai/article/details/53144126 更多参考:http://blog.csdn.net/mafeiyu80/arti ...

  8. [POJ3613] Cow Relays(Floyd+矩阵快速幂)

    解题报告 感觉这道题gyz大佬以前好像讲过一道差不多的?然鹅我这个蒟蒻发现矩阵快速幂已经全被我还给老师了...又恶补了一遍,真是恶臭啊. 题意 给定一个T(2 <= T <= 100)条边 ...

  9. [日常摸鱼]HDU2157 How many ways??

    hhh我又开始水题目了 题意:给一张有向图,多次询问一个点到另一个点刚好走$k$步的方案数取模,点数很小 每个$a,b,k$的询问直接把邻接矩阵$map$自乘$k$次后$map[a][b]$就是答案了 ...

随机推荐

  1. JPA 映射单向多对一的关联关系

    1.首先在多的一端加入一的一端的实体类 //映射单向n-1的关联关 //使用@ManyToOne 来映射多对一的关系 //使用@JoinColumn 来映射外键/可以使用@ManyToOne的fetc ...

  2. 总结:PyQt5自定义信号源

    定义一个信号源有4个方面要注意: 1.定义信号源 A = pyqtSignal([str], [int,str]) 这里特别使用信号源重载的情况加以说明.如上就是信号源A的重载,一个可以发送str参数 ...

  3. 路由知识 静态路由 rip eigrp ospf

    第1章 路由选择原理 1.1 几个概念 1.1.1 被动路由协议 用来在路由之间传递用户信息 1.1.2 主动路由协议 用于维护路由器的路由表 R2#show ip route Codes: C - ...

  4. IdentityServer4 SigningCredential(RSA 证书加密)

    IdentityServer4 默认提供了两种证书加密配置: services.AddIdentityServer() .AddDeveloperSigningCredential() .AddTem ...

  5. Linux替换命令

    :s/^.*$/\L&/100 ##将100行内的小写转换成大写 vi/vim 中可以使用 :s 命令来替换字符串. :s/vivian/sky/ 替换当前行第一个 vivian 为 sky ...

  6. Cocoapods安装过程

    1.升级Ruby环境 gem -v gem update --system 如果没有权限去升级Ruby ?就输入 sudo gem update --system 2.换掉Ruby镜像 首先移除现有的 ...

  7. YSlow---基于firebug的插件 ,用于网站页面性能的分析

    YSlow有什么用? YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. YSlow可以分析任何网站,并为每一个规则产生一个整体报告,如果页面可以进行优化,则Y ...

  8. (转)微信开发连接SAE数据库

    原文地址:http://blog.csdn.net/tterminator/article/details/51067130 在读写云数据库MySQL之前,需要说明的是,在新浪云平台上使用数据库时,该 ...

  9. redhat7 邮件服务搭建

    一.先搭建DNS服务,在正向和反向区域文件分别添加以下配置 cd /var/named 目录下 ① vi abc.com.zone 正向区域文件,添加以下内容 @ MX  5 mail.test.cn ...

  10. Scrum Meeting Alpha - 6

    Scrum Meeting Alpha - 6 NewTeam 2017/10/31 地点:主南203 任务反馈 团队成员 完成任务 计划任务 安万贺 完成了个人博客和班级列表部分API的包装 完成个 ...