1012: A MST Problem

时间限制: 1 Sec  内存限制: 32 MB
提交: 63  解决: 33
[提交][状态][讨论版][命题人:外部导入]

题目描述

It is just a mining spanning tree ( 最小生成树 ) problem, what makes you a little difficult is that you are in a 3D space.

输入

The first line of the input contains the number of test cases in the file. And t he first line of each case
contains one integer numbers n(0<n<30) specifying the number of the point . The n next n line s, each line
contain s Three Integer Numbers xi,yi and zi, indicating the position of point i.

输出

For each test case, output a line with the answer, 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
#include<iostream>
#include<cmath>
#include<cstdio>
#define INF 99999999
#define MAXV 100
using namespace std;
typedef struct
{
double edges[100][100];
int n;
int e;
}MatGraph;
struct point
{
int x;
int y;
int z;
}point[100]; MatGraph g; void CreateMat(MatGraph &g, double graph[100][100], int n)
{
int i, j;
g.n = n;
for(i = 0; i < g.n; ++i)
{
for(j = 0; j < g.n; ++j)
{
g.edges[i][j] = graph[i][j];
}
}
} double Prim(MatGraph g, int v)
{
double lowcost[100];
double min_ = INF;
double r = 0;
int i, j, k;
int n = g.n;
for(i = 0; i < n; ++i)
{
lowcost[i] = g.edges[v][i];
}
for(i = 1; i < n; ++i)
{
min_ = INF;
for(j = 0; j < n; ++j)
{
if(lowcost[j] != -1 && lowcost[j] < min_)
{
min_ = lowcost[j];
k = j;
}
}
lowcost[k] = -1;
r = r + min_;
for(j = 0; j < n; ++j)
{
if(g.edges[k][j] < lowcost[j] && g.edges[k][j] != -1)
{
lowcost[j] = g.edges[k][j];
}
}
}
return r;
} int main()
{
double length, ans, result;
double graph[100][100];
int num, i, j, k, l, t, n;
cin >> t;
while(t--)
{
cin >> n;
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
graph[i][j] = INF;
}
}
for(i = 0; i < n; ++i)
{
cin >> point[i].x >> point[i].y >> point[i].z;
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
length = sqrt((point[i].x - point[j].x)*(point[i].x - point[j].x) + (point[i].y - point[j].y)*(point[i].y - point[j].y) + (point[i].z - point[j].z)*(point[i].z - point[j].z));
graph[i][j] = graph[j][i] = length;
}
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
if(i == j)
{
graph[i][j] = -1;
}
}
}
CreateMat(g, graph, n);
result = Prim(g, 0);
printf("%.2lf\n", result);
}
return 0;
}

  

 

最新文章

  1. Android 面试题--Activity
  2. Ceph性能优化总结(v0.94)
  3. 全键盘Vimium快捷键学习记录
  4. SQL 多表一起查询的语句总结
  5. 关于Java的File.separator
  6. 封装NPOI导出含下拉列表的Excel
  7. Objective-C学习篇04—多态
  8. textview设置不同字体大小
  9. hystrix 结果缓存机制(5)
  10. js中函数表达式和自执行函数表达式的用法总结
  11. 使用pdf.js预览实现读取服务器外部文件
  12. 转://如何创建ASM磁盘
  13. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)
  14. Tomcat 部署项目的三种方法(转)
  15. Python实例---模拟微信网页登录(day3)
  16. Gallery和自定义Adapter配合使用,实现图片预览
  17. OVS
  18. FQ:从入门到放弃(二)
  19. MdelForm 和formset
  20. p2 钢体

热门文章

  1. Gone Fishing
  2. 问题:jQuery中遍历XML文件时候,获取子节点children不支持的情况(已解决)
  3. java——重载解析、静态绑定、动态绑定
  4. 时间戳date 命令
  5. oracle 12.1.0.2的mgmt 导致的ORA-01017 bug
  6. WebStorm 预览时把浏览器地址localhost 改成IP
  7. (转)Shell学习之Shift的用法
  8. LeetCode 454.四数相加 II(C++)
  9. hduoj 2062Subset sequence
  10. oracle expdp impdp 数据泵方式