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;
}

  

 

1012: A MST Problem的更多相关文章

  1. YTU 1012: A MST Problem

    1012: A MST Problem 时间限制: 1 Sec  内存限制: 32 MB 提交: 7  解决: 4 题目描述 It is just a mining spanning tree ( 最 ...

  2. 专题练习HDU题集 图论

    [图论01]最短路 Start Time : 2018-01-02 12:45:00    End Time : 2018-01-23 12:45:00 Contest Status : Runnin ...

  3. D. Design Tutorial: Inverse the Problem 解析含快速解法(MST、LCA、思維)

    Codeforce 472D Design Tutorial: Inverse the Problem 解析含快速解法(MST.LCA.思維) 今天我們來看看CF472D 題目連結 題目 給你一個\( ...

  4. HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)

    6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...

  5. HDU 6330.Problem L. Visual Cube-模拟到上天-输出立方体 (2018 Multi-University Training Contest 3 1012)

    6330.Problem L. Visual Cube 这个题就是输出立方体.当时写完怎么都不过,后来输出b<c的情况,发现这里写挫了,判断失误.加了点东西就过了,mdzz... 代码: //1 ...

  6. NOI模拟题4 Problem A: 生成树(mst)

    Solution 我们考虑答案的表达式: \[ ans = \sqrt{\frac{\sum_{i = 1}^{n - 1} (w_i - \overline{w})^2}{n - 1}} \] 其中 ...

  7. Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS

    题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...

  8. Problem : 1012 ( u Calculate e )

    /*tips:本题只有输入,没有输出,在线测试只检测结果,所以将前面几个结果罗列出来就OK了.为了格式输出问题纠结了半天,最后答案竟然还是错的....所以啊,做题还是得灵活变通.*/ #include ...

  9. noip2017集训测试赛(三)Problem C: MST

    题面 Description 给定一个n个点m条边的连通图,保证没有自环和重边.对于每条边求出,在其他边权值不变的情况下,它能取的最大权值,使得这条边在连通图的所有最小生成树上.假如最大权值为无限大, ...

随机推荐

  1. java——io、字节流缓冲区拷贝文件、字节缓冲流

    使用try catch finally关闭文件流: 写入文件: import java.io.*; public class exp{ public static void main(String[] ...

  2. 转 Django中的Form

    https://www.cnblogs.com/chenchao1990/p/5284237.html Form 一.使用Form Django中的Form使用时一般有两种功能: 1.生成html标签 ...

  3. Python APIs

    NDArray API Python API速查

  4. powerdesigner 遇到的各种问题总结

    1. 设置自增 打开表 -- 在具体列的前面双击即可添加各种属性 2. 生成sql 时设置编码 database --> generate database --> format --&g ...

  5. 夜神模拟器连不上adb的解决办法

    解决办法: 1.任务管理器里看下,adb.exe以及nox_adb.exe这2个进程有没有在运行?有的话就结束掉 2.找到开发环境的SDK的目录和夜神模拟器的目录,将SDK\platform-tool ...

  6. [转]jQuery插件写法总结以及面向对象方式写法

    本文转自:http://www.xuanfengge.com/jquery-plug-in-written-summary-and-summary-of-writing-object-oriented ...

  7. [转]How to Create Custom Filters in AngularJs

    本文转自:http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction F ...

  8. netbeans 窗体字体大小设置

    当计算机分辨率变大的时候,打开netbeans的时候,字体就会变得越来越小 看起来很不爽,所要就要改变一下,窗体字体大小. 在网上找到了一段修改netbeans窗体字体大小的配置信息,现标记起来,以便 ...

  9. ab压测工具的一些个人见解

    ab压测工具(linux版)由于网上教程一大把,今天也按照教程好好研究了一番,下面写一下对此工具的一些个人见解,如有不妥,希望一起探讨.   优点: 1.小巧. 2.理论支持655350并发数.实际3 ...

  10. linux下文件比对功能

    很想对吧两个文本有什么不同,可linux下有没有那么方便的工具,怎么办?其实也很简单:diff命令,一行搞定. 新建a.txt文件