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. jdbc链接hive报错:java.lang.ClassNotFoundException: org.apache.thrift.transport.TTransport

    写了个jdbc连接hive2的demo,结果报错:java.lang.ClassNotFoundException: org.apache.thrift.transport.TTransport,实际 ...

  2. 关于docker使用的几个小问题(一)

    由于刚接触docker踩了几个坑,希望本文对网瘾少年有所帮助. Docker分CE版(社区版)和EE版(商用版),具体安装流程参考文档介绍,在此不再赘述.下面分Windows和Linux分别踩坑: 一 ...

  3. Android插件化-RePlugin项目集成与使用

    前言:前一段时间新开源了一种全面插件化的方案-- RePlugin,之前一种都在关注 DroidPlugin 并且很早也在项目中试用了,但最终没有投入到真正的生产环节,一方面是项目中没有特别需要插件化 ...

  4. 【Spring】渲染Web视图

    前言 前面学习了编写Web请求的控制器,创建简单的视图,本篇博文讲解控制器完成请求到结果渲染到用户的浏览器的过程. 渲染Web视图 理解视图解析 前面所编写的控制器方法都没有直接产生浏览器中渲染所需要 ...

  5. JavaNIO缓冲区

    package com.nio.test; import java.nio.ByteBuffer; import org.junit.Test; /** * * @author fliay * * 一 ...

  6. 微服务下的契约测试(CDC)解读

    1. 前言 有近两周没有在公众号中发表文章了,看过我之前公众号的读者都知道,公众号中近期在连载<RobotFramework接口自动化系列课程>,原本计划每周更新一篇,最近由于博主在带一个 ...

  7. ldap数据库--ODSEE--安装

    在安装之前最好查看一下服务器硬件是否满足要求,是否需要更改一些系统配置来达到使用ldap数据库的最有性能.实际使用的ldap数据库是oracle的产品,DS70即ODSEE. 安装环境:solaris ...

  8. Python 解LeetCode:367. Valid Perfect Square

    题目描述:给出一个正整数,不使用内置函数,如sqrt(),判断这个数是不是一个数的平方. 思路:直接使用二分法,貌似没啥好说的.代码如下: class Solution(object): def is ...

  9. jquery实现抽奖小游戏

    在很多网站或游戏活动中我们都会看到有关抽奖的活动或界面: 下面我将给大家介绍下如何通过javascript来实现这样的一个抽奖功能,主要从下面三个步骤入手(文中着重介绍第三部分有关功能的实现): 1. ...

  10. jenkins+docker 持续构建非docker in docker jenkins docker svn maven

    工欲善其事必先利其器,为了解脱程序员的,我们程序员本身发明了很多好用的工具,通过各种工具的组合来达到我们想要的结果 本文采用jenkins docker svn maven作为相关工具,项目sprin ...