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 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- AI翻译离无障碍交流有多远
AI翻译服务通过硬件.软件连接千千万万个应用场景,会打破语言不通的尴尬局面吗?会是人工翻译的终结者吗? 世界这么大,我想去看看!十一长假临近,梦想中的你背起行囊,自由行走在异国的大街小巷.然而现实的画 ...
- 快速部署PostgreSQL
PostgreSQL通常也简称Postgres,是一个关系型数据库管理系统,适用于各种Linux操作系统.Windows.Solaris.BSD和Mac OS X.本文基于CentOS7,使用yum源 ...
- [译]ASP.NET Core 2.0 视图引擎
问题 如何在ASP.NET Core 2.0中使用Razor引擎来创建视图? 答案 新建一个空项目,修改Startup.cs,添加MVC服务和请求中间件: public void ConfigureS ...
- Linux库函数制作(静态库、动态库)
Linux库函数制作(静态库.动态库) 静态库与动态库 链接方式 链接分为两种:静态链接.动态链接 静态链接: 由链接器在链接时将库的内容加入到可执行程序中 静态链接的特点是: 优点: 对运行环境的依 ...
- python3 字符串操作相关函数
整理自python基础|菜鸟教程 感谢菜鸟教程提供的优质资源! 1.capitalize() 将字符串的第一个字符转换为大写 实例 以下实例展示了capitalize()方法的实例: #!/usr/b ...
- XMLHttpRequest2 异步 ajax
XMLHttpRequest1只是对已经存在的xhr对象细节进行规范定义, XMLHttpRequest2升级了该对象. FormData 类型可以用在xhr传输的时候,把表单序列化或者将数据以表 ...
- struts2运行过程(图解)
.................................................................................................... ...
- 安装MongoDB启动时报错‘发生系统错误2’的解决办法
安装数据库mongodb启动时报"发生系统错误2". 这个问题是如果你之前已经装过一次,并且两次安装目录不同,就绝对会碰到的,因为你之前安装的路径已经在注册表中生成了,并没有随着你 ...
- Three ways to throw exception in C#. Which is your preference?
There are three ways to 'throw' a exception in C# C#中有三种抛出异常的方式 Use the throw keyword without an id ...