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. yii1的后台分页和列表

    控制器: public function actionIndex(){ $model = new Cases('search'); $model->unsetAttributes(); // c ...

  2. Angular JS ng-repeat 报错 Error: [ngRepeat:dupes]

    ng-repeat常用情况: <div class="form-group" ng-repeat="item in items"></div& ...

  3. maven 环境变量配置

    新增变量:MAVEN_HOME:D:\tools\apache-maven-3.52 修改path,在path中新增如下: %MAVEN_HOME\bin%; 注意末尾增加分号 最后,到dos中查看m ...

  4. 用python处理时间、utf8文本、正则匹配、序列化、目录路径搜索、xml解析

    python 处理时间 import time import re now = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) ...

  5. ThreadLocal 开启事务

    1.ThreadLocal该类提供了线程局部变量 2.分析原理: ThreadLocal内部有一个Map.Map的key是当前线程对象,value是一个Object对象. 模拟该类: public c ...

  6. WPF调用Win Form

    WPF是win form的下一代版本,现在越来越多的公司使用WPF.如何兼容已有的使用win form开发的应用程序呢?下面有三种方式来在WPF中调用win form. 使用WPF中的WindowsF ...

  7. Kudu的优点

    不多说,直接上干货! Kudu目前具有以下优点  OLAP 工作的快速处理: 与 MapReduce,Spark 和其他 Hadoop 生态系统组件集成: 与 Apache Impala(incuba ...

  8. verilog if语句

    a.基本形式 1) if(表达式) 语句1: 2)if(表达式) 语句1: else 语句1 3) if(表达式1) 语句1: else   if(表达式2) 语句2: else   if(表达式3) ...

  9. 批量处理标签属性中document.getElementsByName()的替代方案

    背景 今天在逛知乎时候,看到一个JavaScript方面的问题: 最近在学习JavaScript DOM,就好奇地查阅资料,以及请教学长,得到下面解答: http://www.w3help.org/z ...

  10. PHP+jQuery实现翻板抽奖(中奖概率算法)

    在电视节目中有一种抽奖形式暂且叫做翻板抽奖,台上有一个墙面,墙面放置几个大方块,主持人或者抽奖者翻开对应的方块即可揭晓中奖结果.类似的抽奖形式还可以应用在WEB中,本文将使用PHP+jQuery为您讲 ...