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. C++ vector类型要点总结(以及各种algorithm算法函数)

    概述 C++内置的数组支持容器的机制,但是它不支持容器抽象的语义.要解决此问题我们自己实现这样的类.在标准C++中,用容器向量(vector)实现. 容器向量也是一个类模板.vector是C++标准模 ...

  2. VS 2017与 Docker

    https://www.cnblogs.com/rufus-hua/p/6742836.html 参考 1 基于VS2017的Docker Support体检ASP.NET Core站点的Docker ...

  3. ubuntu安装软件依赖解决

    sudo apt-get install -f zsh@zsh:~/Downloads/dist$ sudo dpkg --install Kitematic_0.17.3_amd64.deb (正在 ...

  4. [转]win系统下nodejs安装及环境配置

    本文转自:http://www.cnblogs.com/linjiqin/p/3765390.html 第一步:下载安装文件 下载nodejs,官网:http://nodejs.org/downloa ...

  5. [转]JS 只能输入数字和两位小数的JS

    本文转自:http://blog.sina.com.cn/s/blog_724008890101dgep.html JS代码: <script language="JavaScript ...

  6. jQuery源码解读 --- 整体架构

    最近学习比较忙,感觉想要提高还是要读源码,所以准备考试这个考试结束就开始读jquery源码啦,加油~

  7. http学习笔记(三):报文

                             三.报文 目录: 3.1方法 1.get 2.head 3.put 4.post 5.trace 6.options 7.delete 3.2状态码 ...

  8. css实现高度垂直居中

    1:单行文字垂直居中: 如果一个容器中只有一行文字的话,定义height(高度)和 line-height(行高)相等即可. 如:<div style="height:25px;lin ...

  9. php fopen()和file_get_contents() 区别介绍

    本文章向码农们介绍PHP使用fopen与file_get_contents读取文件实例分享及这两个函数的区别,需要的码农可以参考一下. php中读取文件可以使用fopen和file_get_conte ...

  10. JEECMS站群管理系统-- 标签的配置流程

    以cms_content_list为例,首先,每一个标签的声明都是在jeecms-context.xml中进行的, <?xml version="1.0" encoding= ...