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. Spring MVC自定义错误页面

    在web.xml中添加: <error-page(其他属性404...省略咯)> <location>/error</location> </error-pa ...

  2. Checkstyle的配置详解

    Checkstyle是一款检查java程序代码样式的工具,可以有效的帮助我们检视代码以便更好的遵循代码编写标准,特别适用于小组开发时彼此间的样式规范和统一.Checkstyle提供了高可配置性,以便适 ...

  3. Spring---数据缓存(未完待续)

    1.为什么需要数据缓存? 程序的瓶颈大都在数据库,而内存的速度是远远大于硬盘的,当我们需要重复读取相同数据时,一次又一次的请求数据库或者远程服务,导致大量的时间浪费在数据库或者 远程服务上,导致程序性 ...

  4. sqlserver 2012 部署详解

    01,下载 官网下载: https://www.microsoft.com/zh-cn/download/details.aspx?id=29066 02,安装 检查系统环境配置 成功了就继续,其他的 ...

  5. 3d Max 2013安装失败怎样卸载3dsmax?错误提示某些产品无法安装

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  6. SourceTree 关于 .gitignore使用/下载

    # =============== # # Unity generated # # =============== # Temp/ Obj/ UnityGenerated/ Library/ Asse ...

  7. 性能测试工具LoadRunner05-LR之Virtual User Generator html模式与url模式

    “HTML-based script”说明 在默认情况下,选择“HTML-based script”,说明脚本中采用HTML页面的形式来表示,这种方式的脚本容易维护,容易理解,推荐这种方式录制 “UR ...

  8. Android Studio中导入v4,v7和recyclerview-v7包的方法

    概述 Android Studio中新建工程中会默认导入v7的包,即在gradle中默认配置了com.android.support:appcompat-v7:x.x.x的依赖.但是不会默认为我们配置 ...

  9. (转)cut命令详解

    Linux:cut命令详解   cut 文件内容查看 显示行中的指定部分,删除文件中指定字段 显示文件的内容,类似于下的type命令. 说明 该命令有两项功能,其一是用来显示文件的内容,它依次读取由参 ...

  10. CI模板中php脚本的使用

    今天偶然发现,在CI的模板中能够直接使用CI自带的函数,并且可以直接调用controller里面的属性.案例: 控制器: public function test(){ $this->a = ' ...