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. MongoDB自动增长

    MongoDB 没有像SQL一样有自动增长的功能,如果我们需要实现ObjectId自动增长功能,可以通过编程的方式来实现.步骤如下: 1. 创建counters集合: db.createCollect ...

  2. SQL查询一个表里面某个字段值相同的数据记录

    好长时间没有用SQL了...还停留在学生时代的水平... 转: 昨天遇到个面试题:查询一个表里面某个字段值相同的数据记录,好久没有写过这种,还真的花了点时间才写出来.如表g_carddetail,有 ...

  3. thinkphp 默认首页 更改

    原thinkphp的默认首页为:Home/index,如果想更改,则需要配置: 在Common/config之下 'DEFAULT_CONTROLLER' => 'Admin', // 更改后默 ...

  4. jQuery源码的一个坑

    纯吐槽 大半夜也真是够了,想学着jQ造个小轮子巩固下js,结果一开始就卡住了. 虽然之前也看过源码,但是主要是研究方法实现什么的,对于框架主函数和入口结构不怎么熟悉,于是想着一步一步调试看看. $(' ...

  5. 三目运算的使用&bytes类型转str类型

    一.三目运算的使用 就像c语言中有三目运算符一样,python中也有三目运算符,废话不多说直接上代码 a=3 c=4 b=a if a>c else c print(b) 意思就和 if a&g ...

  6. 0_Simple__inlinePTX + 0_Simple__inlinePTX_nvrtc

    在核函数代码中加入并行线程执行(Parallel Thread eXecution,PTX),通过汇编指令获取得有关线程束的信息.并且在静态代码和运行时编译两种条件下使用. ▶ 源代码:静态使用 #i ...

  7. js和jquery实现监听键盘事件

    一.使用javascript实现 <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...

  8. python利用django实现简单的登录和注册,并利用session实现了链接数据库

    利用session实现与数据库链接,登录模块(在views.py) def login(request): # return HttpResponseRedirect('/') # 判断是否post方 ...

  9. Spring AOP分析(3) -- CglibAopProxy实现AOP

    上文探讨了应用JDK动态代理实现Spring AOP功能的方式,下面将继续探讨Spring AOP功能的另外一种实现方式 -- CGLIB. 首先,来看看类名CglibAopProxy,该类实现了两个 ...

  10. form提交地址地址正确但是依旧报错404找不到路径

    ---恢复内容开始--- 我的jsp中保含了">="和"<="符号,form提交的时候会有个标签校验,如下: private static bool ...