Lonlife 1000 - Spoon Devil's 3-D Matrix
Time Limit:1s Memory Limit:32MByte
Submissions:208Solved:65
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.
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
思路:
三维点的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的更多相关文章
- Spiral Matrix(LintCode)
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- 《转》循环神经网络(RNN, Recurrent Neural Networks)学习笔记:基础理论
转自 http://blog.csdn.net/xingzhedai/article/details/53144126 更多参考:http://blog.csdn.net/mafeiyu80/arti ...
- 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 ...
- (数据科学学习手札16)K-modes聚类法的简介&Python与R的实现
我们之前经常提起的K-means算法虽然比较经典,但其有不少的局限,为了改变K-means对异常值的敏感情况,我们介绍了K-medoids算法,而为了解决K-means只能处理数值型数据的情况,本篇便 ...
- <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 ...
- 【算法导论】--分治策略Strassen算法(运用下标运算)【c++】
由于偷懒不想用泛型,所以直接用了整型来写了一份 ①首先你得有一个矩阵的class Matrix ②Matrix为了方便用下标进行运算, Matrix的结构如图:(我知道我的字丑...) Matrix. ...
- 循环神经网络RNN
转自 http://blog.csdn.net/xingzhedai/article/details/53144126 更多参考:http://blog.csdn.net/mafeiyu80/arti ...
- [POJ3613] Cow Relays(Floyd+矩阵快速幂)
解题报告 感觉这道题gyz大佬以前好像讲过一道差不多的?然鹅我这个蒟蒻发现矩阵快速幂已经全被我还给老师了...又恶补了一遍,真是恶臭啊. 题意 给定一个T(2 <= T <= 100)条边 ...
- [日常摸鱼]HDU2157 How many ways??
hhh我又开始水题目了 题意:给一张有向图,多次询问一个点到另一个点刚好走$k$步的方案数取模,点数很小 每个$a,b,k$的询问直接把邻接矩阵$map$自乘$k$次后$map[a][b]$就是答案了 ...
随机推荐
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- H5前端上传文件的几个解决方案
目前,几个项目中用到了不同的方法,总结一下分享出来. 第一种,通过FormData来实现. 首先,添加input控件file. <input type="file" name ...
- 移动端通过ajax上传图片(文件)并在前台展示——通过H5的FormData对象
前些时候遇到移动端需要上传图片和视频的问题,之前一直通过ajax异步的提交数据,所以在寻找通过ajax上传文件的方法.发现了H5里新增了一个FormData对象,通过这个对象可以直接绑定html中的f ...
- 聊聊 Material Design 里,阴影的那些事儿!
当你的设计师要求你在某个 View 上增加阴影效果,那你只需要认真阅读本文,阴影的问题就不再是问题. 一.前言 设计师的世界,与常人不同,有时候想要扁平化的风格,有时候又想要拟物化的风格.而在 Mat ...
- Windows环境下Android Studio安装和使用教程
Windows环境下Android Studio安装和使用教程 来源: http://www.cnblogs.com/liuhongfeng/archive/2015/12/30/5084896.ht ...
- 最最简单的CentOs6在线源搭建
非常实用的在线源搭建,只要4步骤 1.点击进入http://mirrors.aliyun.com/repo/epel-6.repo ,这是阿里云的源 2.复制所有的代码 ctrl+a,ctrl+c ...
- P3003 [USACO10DEC]苹果交货Apple Delivery
题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...
- SQL Server 2008对日期时间类型的改进
微软在备受多年的争议后,终于对日期时间数据类型开刀了,在新版的SQL Server 2008中一口气增加了4种新的日期时间数据类型,包括: Date:一个纯的日期数据类型. Time:一个纯的时间数据 ...
- riot.js教程【三】访问DOM元素、使用jquery、mount输入参数、riotjs标签的生命周期
前文回顾 riot.js教程[二]组件撰写准则.预处理器.标签样式和装配方法 riot.js教程[一]简介 访问DOM元素 你可以通过this.refs对象访问dom元素 而且还有大量的属性简写方式可 ...
- [深度学习]实现一个博弈型的AI,从五子棋开始(2)
嗯,今天接着来搞五子棋,从五子棋开始给小伙伴们聊AI. 昨天晚上我们已经实现了一个五子棋的逻辑部分,其实讲道理,有个规则在,可以开始搞AI了,但是考虑到不够直观,我们还是顺带先把五子棋的UI也先搞出来 ...