1012: A MST Problem

时间限制: 1 Sec  内存限制: 32 MB

提交: 7  解决: 4

题目描述

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>
#include<cstring>
using namespace std;
const int infinity=99999999;
const int maxnum=105;
double map1[maxnum][maxnum];
bool visited[maxnum];
double low[maxnum];
int nodenum;
struct node
{
int x;
int y;
int z;
} nd[105];
double prim()
{
int i,j,pos=1;
double result,Min;
memset(visited,0,sizeof(visited));//初始化都未标记
result=0;
for(i=1; i<=nodenum; i++)
low[i]=map1[pos][i];
visited[pos]=1;//把1号作为起点
for(i=2; i<=nodenum; i++) //这个i没有其他的意思就是一个循环次数
{
Min=infinity;
pos=-1;//从1号开始找最小的边
for(j=1; j<=nodenum; j++)
if(!visited[j]&&Min>low[j])
{
Min=low[j];
pos=j;
}
if(pos==-1)
return -1;
visited[pos]=1;//做到与1os号最近的边
result+=Min;//加权值
for(j=1; j<=nodenum; j++)
if(!visited[j]&&low[j]>map1[pos][j])
low[j]=map1[pos][j];//这个就是替换未被标记的最小权值!
}
return result;
}
int main()
{
int n,i,j,t;
double lenth,ans;
cin>>t;
while(t--)
{
cin>>n;
nodenum=n;
for(i=1; i<=nodenum; i++)
for(j=1; j<=nodenum; j++)
map1[i][j]=infinity;
for(i=1; i<=n; i++)
cin>>nd[i].x>>nd[i].y>>nd[i].z;
for(i=1; i<=n; i++)
{
for(j=i+1; j<=n; j++)
{
lenth=sqrt((nd[i].x-nd[j].x)*(nd[i].x-nd[j].x)+(nd[i].y-nd[j].y)*(nd[i].y-nd[j].y)+(nd[i].z-nd[j].z)*(nd[i].z-nd[j].z));
// printf("djklsajiofgioj%.2lf\n",lenth);
map1[i][j]=map1[j][i]=lenth;
}
}
ans=prim();//开始rim算法
if(ans==-1)
cout<<"?"<<endl;
else
printf("%.2lf\n",ans);
}
}

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

  1. 1012: A MST Problem

    1012: A MST Problem 时间限制: 1 Sec  内存限制: 32 MB提交: 63  解决: 33[提交][状态][讨论版][命题人:外部导入] 题目描述 It is just a ...

  2. ACM YTU 1012 u Calculate e

    u Calculate e Problem Description A simple mathematical formula for e is where n is allowed to go to ...

  3. ACM YTU 挑战编程 字符串 Problem A: WERTYU

    Problem A: WERTYU Description A common typing error is to place yourhands on the keyboard one row to ...

  4. YTU 1001: A+B Problem

    1001: A+B Problem 时间限制: 1 Sec  内存限制: 10 MB 提交: 4864  解决: 3132 [提交][状态][讨论版] 题目描述 Calculate a+b 输入 Tw ...

  5. 专题练习HDU题集 图论

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

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

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

  7. 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 - ...

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

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

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

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

随机推荐

  1. IntrospectorCleanupListener监听器防止内存溢出

    <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</ ...

  2. MyBatis 3 分页

    利用MyBatis 3进行分页,选定的数据库表c_province,有3列,id列,provinceid列,province列,用Oracle数据库.首先建立一个对应的实体类,Province有3个属 ...

  3. asp.net mvc数据验证

    文章:asp.net mvc3 的数据验证(一) 文章:ASP.NET MVC下的四种验证编程方式 这个讲了在一个地方展示验证信息 文章:[ASP.NET MVC系列]浅谈数据注解和验证 这个在每个输 ...

  4. POJ1780 Code

    KEY公司开发出一种新的保险箱.要打开保险箱,不需要钥匙,但需要输入一个正确的.由n位数字组成的编码.这种保险箱有几种类型,从给小孩子玩的玩具(2位数字编码)到军用型的保险箱(6位数字编码).当正确地 ...

  5. hdu3853:LOOPS

    题目大意:r*c个点,每个点有Aij的概率回到自己本身,Bij的概率向右一格,Cij的概率向下一格,求从(1,1)到(r,c)的期望步数. 题解:有了hdu4405的经验,从后往前推期望.那么,E(i ...

  6. Codeforces Round #296 (Div. 2) C. Glass Carving [ set+multiset ]

    传送门 C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. HDU 1041

    题意: 给原始序列1 给定变化规则是,对于原来的序列每一个0前边插入1,每个1前边插入0. 问原始序列经过n次变化之后有多少对相邻的0. 规律题: 从第二次开始 当第奇数次变化之后,数量变成原来数量的 ...

  8. 开头第一篇Hello World

    以前在折腾个人博客的时候,使用过的WordpPress.Z-Blog.Typecho建站程序,开头第一篇都是Hello World,作为程序员的社区,开头第一篇当然也要是Hello World! 一句 ...

  9. MySQL中的数据类型的长度范围和显示宽度(转)

    长度范围是随数据类型就已经是固定的值,而显示宽度与长度范围无关. 以下是每个整数类型的存储和范围(来自MySQL手册) 类型 字节 最小值 最大值 (带符号的/无符号的) (带符号的/无符号的) TI ...

  10. 框架-Jquerychange事件数值计算

    //优惠率计算优惠价            $("body").on("change", "#Rate", function() {     ...