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}} \] 其中 ...
随机推荐
- LINUX常用文件说明
一.网络配置 1.修改主机名 /etc/sysconfig/network或/etc/hosts 2.开启或关闭网卡 ifconfig eth0 down/up 3.linuxa下查看一个网卡绑定的所 ...
- 返回json格式的数据
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便
A. Boy or Girl time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- [转]ORA-38500: USING CURRENT LOGFILE option not available without stand
标签: oracle 10g 数据库 ora-38500 it 分类: IT author:skate time :2009/08/03 在dataguard启用实时恢复的时候,报如下错误: ORA- ...
- 洛谷P2870 - [USACO07DEC]最佳牛线Best Cow Line
Portal Description 给出一个字符串\(s(|s|\leq3\times10^4)\),每次从\(s\)的开头或结尾取出一个字符接在新字符串\(s'\)的末尾.求字典序最小的\(s'\ ...
- C语言一些常见的操作字符串方法
// 首字母大写 ;(c=string[i])!='\0';i++) // 字符串碰到\0结束 C知识 { if (c==' ') { printf("%c",c), word=; ...
- Xcode warning:Auto property synthesis will not synthesize property
iOS 警告提示如下: 添加 @dynamic告诉编译器这个属性是动态的,动态的意思是等你编译的时候就知道了它只在本类合成; 如下:
- wordpress网站后台打开速度很慢解决方法?
今天就和朋友们分享下,wordpress网站后台最近打开速度很慢的原因及解决方法.推荐第三种方法 方法/步骤 1.安装插件:在插件中搜索 Disable Google Fonts,选择安装,然后启 ...
- Eventquery.vbs
https://docs.microsoft.com/en-us/previous-versions/orphan-topics/ws.10/cc772995(v=ws.10)
- cobbler ks文件解释--转载
cobbler中ks.cfg文件配置详解 许多系统管理员宁愿使用自动化的安装方法来安装红帽企业 Linux.为了满足这种需要,红帽创建了kickstart安装方法.使用kickstart ...