# Lonlife-ACM 1000 - Spoon Devil's 3-D Matrix

Accept: 0    Submit: 0
Time Limit: 1s    Memory Limit : 32MByte

## Problem 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.

2
2
1 1 0
2 2 0
3
1 2 3
0 0 0
1 1 1

1.41
3.97

## Problem Idea

【题意】

【类型】

【分析】

prim：任选一个结点放入点集S中，一般选取结点1，即S={1}，每次挑选与点集S直接相邻的边权最小的且不属于点集S的结点加入点集S中，直到所有的点都加入点集S内。

kruskal：每次选取未在边集E中的最短边加入边集E中，若选取的边使得边集内的图存在回路，则丢弃该边，直到每个点都在边集内

【时间复杂度&&优化】
kruskal:O(ElogE) prim+邻接矩阵:O(V*V) prim+邻接表:O(ElogV)

## Source Code

/*Sherlock and Watson and Adler*/
//Prim + 邻接表
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 55;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
struct node
{
double x,y,z;
}s[N];
struct edge
{
int v,to;
double d;
bool operator < (const edge &a) const
{
return d>a.d;//最小值优先
}
}e[N*N];
int h[N],p;
bool v[N];
{
e[p].v=v;
e[p].d=d;
e[p].to=h[u];
h[u]=p++;
e[p].v=u;
e[p].d=d;
e[p].to=h[v];
h[v]=p++;
}
double distant(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
int main()
{
int t,n,i,j,k;
double ans;
edge u;
priority_queue<edge> q;
scanf("%d",&t);
while(t--)
{
p=0;ans=0;
while(!q.empty())
q.pop();
memset(h,-1,sizeof(h));
memset(v,false,sizeof(v));
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%lf%lf%lf",&s[i].x,&s[i].y,&s[i].z);
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
v[1]=true;k=1;
for(i=h[1];i+1;i=e[i].to)
if(!v[e[i].v])
q.push(e[i]);
while(k!=n)
{
u=q.top();
q.pop();
if(v[u.v])
continue;
v[u.v]=true;
k++;
ans+=u.d;
for(i=h[u.v];i+1;i=e[i].to)
if(!v[e[i].v])
q.push(e[i]);
}
printf("%.2f\n",ans);
}
return 0;
}

/*Sherlock and Watson and Adler*/
//Prim + 邻接矩阵
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 55;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
struct node
{
double x,y,z;
}s[N];
struct edge
{
int v;
double d;
edge(){}
edge(int _v,double _d):v(_v),d(_d){}
bool operator < (const edge &a) const
{
return d>a.d;//最小值优先
}
};
double g[N][N];
bool v[N];
double distant(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
int main()
{
int t,n,i,j,k;
double ans;
edge u;
priority_queue<edge> q;
scanf("%d",&t);
while(t--)
{
ans=0;
while(!q.empty())
q.pop();
memset(v,false,sizeof(v));
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%lf%lf%lf",&s[i].x,&s[i].y,&s[i].z);
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
g[i][j]=g[j][i]=distant(s[i],s[j]);
v[1]=true;k=1;
for(i=1;i<=n;i++)
if(!v[i])
q.push(edge(i,g[1][i]));
while(k!=n)
{
u=q.top();
q.pop();
if(v[u.v])
continue;
v[u.v]=true;
k++;
ans+=u.d;
for(i=1;i<=n;i++)
if(!v[i])
q.push(edge(i,g[u.v][i]));
}
printf("%.2f\n",ans);
}
return 0;
}

/*Sherlock and Watson and Adler*/
//Kruskal
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 55;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
struct node
{
double x,y,z;
}s[N];
struct edge
{
int u,v;
double d;
}e[N*N/2];
bool cmp(edge x,edge y)
{
return x.d<y.d;
}
int c[N];
int fun(int x)
{
if(c[x]!=x)
c[x]=fun(c[x]);
return c[x];
}
double distant(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
int main()
{
int t,n,i,j,p;
double ans;
scanf("%d",&t);
while(t--)
{
ans=0;p=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%lf%lf%lf",&s[i].x,&s[i].y,&s[i].z);
c[i]=i;
}
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
e[p].u=i;e[p].v=j;
e[p++].d=distant(s[i],s[j]);
}
sort(e,e+p,cmp);
for(i=0;i<p;i++)
{
if(fun(e[i].u)==fun(e[i].v))
continue;
c[fun(e[i].u)]=fun(e[i].v);
ans+=e[i].d;
}
printf("%.2f\n",ans);
}
return 0;
}

#### Lonlife-ACM 1005 - Spoon Devil's RP Test(同余定理)——“玲珑杯”acm比赛-试运行赛

2016-10-19 09:49:28

#### 【总结】"浪潮杯"第七届ACM山东省省赛山师场总结

2016-07-09 18:30:54

#### 玲珑学院OJ 1000 Spoon Devil's 3-D Matrix

2016-09-25 13:42:28

#### “玲珑杯”线上赛 Round #17 震惊，99%+的中国人都会算错的问题（容斥原理）

2017-07-03 21:19:39

#### 2017湖南大学ACM程序设计新生杯大赛 - B - Build （并查集+贪心）

2018-01-21 22:36:09

#### 2018第九届山东省ACM省赛

2018-05-07 00:51:45

#### “浪潮杯”山东省第七届ACM大学生程序设计竞赛【C - Proxy】

2016-10-12 10:47:33

#### i春秋百度杯CTF比赛2016年12月场writeup

2017-01-01 21:52:56

#### 1262: 魔法宝石 (“玲珑杯”河南工业大学ACM程序设计大赛)

2017-04-23 20:53:58