Lonlife 1000 - Spoon Devil's 3-D Matrix
来源:互联网 发布:学算法看什么书 编辑:程序博客网 时间:2024/04/26 06:54
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 integerT
-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
SOLUTION
“玲珑杯”acm比赛-试运行赛
思路:
三维点的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;}
1 0
- Lonlife 1000 - Spoon Devil's 3-D Matrix
- Lonlife-ACM 1000 - Spoon Devil's 3-D Matrix(最小生成树)——“玲珑杯”acm比赛-试运行赛
- 玲珑学院OJ 1000 Spoon Devil's 3-D Matrix
- 玲珑学院OJ 1000 - Spoon Devil's 3-D Matrix(3维最小生成树)
- Lonlife-ACM 1005 - Spoon Devil's RP Test(同余定理)——“玲珑杯”acm比赛-试运行赛
- 玲珑杯 1003 Spoon Devil's Diamond Mine
- 玲珑杯 1005 Spoon Devil's RP Test(水题)
- 玲珑杯 1009 Spoon Devil's Bucket 矩阵优化DP
- Spoon Devil Love Arithmetic
- 玲珑学院OJ 1005 - Spoon Devil's RP Test(求余问题)
- 玲珑杯1006 Spoon Devil Love Arithmetic
- 玲珑杯 1007 Spoon Devil Love Game
- 玲珑杯 1008 Spoon Devil Love Arithmetic Again
- 看魔鬼代言人(Devil's Advocate)有感
- The Devil's Guide to Debugging
- convert 3D matrix into diagonal block matrix
- LeetCode7.3(Search a 2D Matrix)
- matrix 实现动画3D旋转效果
- 使用satis 搭建私有Composer
- docker http proxy
- Android SQLite笔记(逐步添加中)
- 矩阵快速幂
- python with as语法
- Lonlife 1000 - Spoon Devil's 3-D Matrix
- 元素分类
- 中国公民身份证号码校验
- css 文本超出2行就隐藏并且显示省略号(适用于Chrome)
- 排序算法-Sorting algorithm
- 如何在Swift3中获取Json包的内容(unwrap Json package)
- Java基础-15总结对象数组,集合Collection,集合List
- python
- 单一职责原则