YTU 1012: A MST Problem
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.
输入
输出
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);
}
}
#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的更多相关文章
- 1012: A MST Problem
1012: A MST Problem 时间限制: 1 Sec 内存限制: 32 MB提交: 63 解决: 33[提交][状态][讨论版][命题人:外部导入] 题目描述 It is just a ...
- 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 ...
- ACM YTU 挑战编程 字符串 Problem A: WERTYU
Problem A: WERTYU Description A common typing error is to place yourhands on the keyboard one row to ...
- YTU 1001: A+B Problem
1001: A+B Problem 时间限制: 1 Sec 内存限制: 10 MB 提交: 4864 解决: 3132 [提交][状态][讨论版] 题目描述 Calculate a+b 输入 Tw ...
- 专题练习HDU题集 图论
[图论01]最短路 Start Time : 2018-01-02 12:45:00 End Time : 2018-01-23 12:45:00 Contest Status : Runnin ...
- D. Design Tutorial: Inverse the Problem 解析含快速解法(MST、LCA、思維)
Codeforce 472D Design Tutorial: Inverse the Problem 解析含快速解法(MST.LCA.思維) 今天我們來看看CF472D 題目連結 題目 給你一個\( ...
- 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 - ...
- HDU 6330.Problem L. Visual Cube-模拟到上天-输出立方体 (2018 Multi-University Training Contest 3 1012)
6330.Problem L. Visual Cube 这个题就是输出立方体.当时写完怎么都不过,后来输出b<c的情况,发现这里写挫了,判断失误.加了点东西就过了,mdzz... 代码: //1 ...
- NOI模拟题4 Problem A: 生成树(mst)
Solution 我们考虑答案的表达式: \[ ans = \sqrt{\frac{\sum_{i = 1}^{n - 1} (w_i - \overline{w})^2}{n - 1}} \] 其中 ...
随机推荐
- Apollo配置中心的使用
1. 自己搭建Apollo配置中心 碰到如下错误: nested exception is org.hibernate.HibernateException: Access to DialectRes ...
- AI学习笔记(01)
[1]在Ai中,每个对象有两个属性:填充颜色和描边颜色. [2]在AI中,非要选中,才操作.而PS中,选中图层即可. [3]AI中,都是路径. [4]选择工具是选中整个路径,而直接选择工具 ...
- Java基础学习总结(91)——阿里巴巴Java开发手册公开版
1.不要嫌名字长 无论是方法,变量,还是函数的取名,不要嫌弃名称太长,只要能够表示清楚含义就可以了. 2.String[] args而不是String args[] 中括号是数组类型的一部分,数组定义 ...
- 三、Oracle常用内置函数
1. ASCII 返回与指定的字符对应的十进制数; SQL> select ascii(A) A,ascii(a) a,ascii(0) zero,ascii( ) space from d ...
- CDOJ 1225 Game Rooms
Game Rooms Time Limit: 4000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Your ...
- Nuget,程序员的功能包
导读:随着信息技术的发展,资源的共享已经成为一种文化.对于程序设计来说,我们在编写软件的时候,总有那么一些公共使用的东西,或者说需啊哟使用到别人可能已经做得很好的东西.这个时候,再去重写,并不是一个明 ...
- CodeForces 610B-Vika and Squares,有坑点,不是很难~~
B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- HDU 3062 简单的2-SAT问题
在2-SAT,最让我纠结的还是添加有向线段的函数了 void add_clause(int i,int a,int j,int b){ int m=2*i+a; int n=2*j+b; ...
- mysql获取行号的方法
1.不排序 语句: ) ) ) b,bigquestion 结果: 2.排序的 语句 ) ) ) b,bigquestion order by bigquestion.bigQuestionSequ ...
- Back弹出AlertDialog
package com.pingyijinren.helloworld.activity; import android.content.DialogInterface; import android ...