YTU 1012: A MST Problem
来源:互联网 发布:软件新品发布会 ppt 编辑:程序博客网 时间:2024/05/16 14:19
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 .
样例输入
221 1 02 2 031 2 30 0 01 1 1
样例输出
1.413.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); }}
1 0
- YTU 1012: A MST Problem
- A MST Problem
- YTU 1001: A+B Problem
- ytu Problem A: A--A Repeating Characters师创杯
- YTU-OJ-Problem A: A代码完善--向量的运算
- ACM YTU 挑战编程 字符串 Problem A: WERTYU
- YTU-OJ-Problem A: 最小节点(线性表)
- YTU-OJ-Problem O: A类设计--正方形
- YTU-OJ-Problem Q: A改错题--体检情况分析
- YTU-OJ-Problem B: A改错题--植物与花
- YTU-OJ-Problem C: A代码完善--哺乳动物
- YTU-OJ-Problem D: A代码完善--系统日期
- YTU-OJ-Problem F: A代码完善--简易二元运算
- YTU-OJ-Problem P: A改错题--学生信息的输入和输出
- YTU 2732:3798-Abs Problem
- YTU 2547: Repairing a Road
- YTU-OJ-Problem E: 类的应用
- YTU-OJ-Problem G: 交通工具信息
- 视频格式转化(将MP4格式转换成ogg格式)
- 关于自定义函数与静态库中函数重名不报错问题的解决
- Qwt6.1.2在windows和ubuntu上的安装配置
- Leetcode 6. ZigZag Conversion
- java文件操作案例
- YTU 1012: A MST Problem
- NYOJ:题目56 阶乘因式分解(一)
- (入门)uva 227 Puzzle
- Father Christmas flymouse--POJ3160Tarjan
- Python--逻辑/循环/异常/编码
- How to Set and Unset Local, User and System Wide Environment Variables in Linux
- 告诉你Hadoop是什么【图解】
- Leetcode 20. Valid Parentheses
- Prime最小生成树