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}} \] 其中 ...
随机推荐
- Android学习2013年10月11日
1.LinearLayout http://www.cnblogs.com/salam/archive/2010/10/20/1856793.html LinearLayout是线性布局控件,它包含的 ...
- python自定义模块导入方法,文件夹,包的区别
python模块导入,网上介绍的资料很多,方法也众说纷纭.根据自己的实践,感觉这个方法最简单直接,而且可以与主流的python ide生成的工程是一样的. 规则只有三条 1. 严格区分包和文 ...
- HDU 5468 Puzzled Elena
Puzzled Elena Time Limit: 2500ms Memory Limit: 131072KB This problem will be judged on HDU. Original ...
- Leetcode 260.只出现一次的数字III
只出现一次的数字III 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 : 输入: [1,2,1,3,2,5] 输出: [3,5 ...
- HDU 3932 模拟退火
HDU3932 题目大意:给定一堆点,找到一个点的位置使这个点到所有点中的最大距离最小 简单的模拟退火即可 #include <iostream> #include <cstdio& ...
- 【HDOJ6299】Balanced Sequence(贪心)
题意:给定n个只有左右括号的序列,要求将它们重新排序使得匹配的括号对数最大. n<=1e5 s[i]<=1e5 sum s[i]<=5e6 思路: 先把每个串内部的匹配数量减去,剩下 ...
- 【BZOJ2560】串珠子(状压DP,容斥原理)
题意: 铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体.现在已知所有珠子互不相同,用整数1到n编号.对于第i个珠子和第j个珠子,可以选择不用绳子连接,或者在 ...
- iOS React Native 环境的搭建
react native 的官网:http://reactnative.cn/docs/0.47/getting-started.html#content --iOS如何搭建mac版的环境 1.配置 ...
- uva 10604
状态压缩 奇怪的是A与B混合 和 B与A 混合得到的热量可能不同 #include <cstdio> #include <cstdlib> #include <cmat ...
- Spring4MVC 请求参数映射和Content-type
目录 前言 不使用注解(不传则为null) 基本数据类型和日期类型 自定义类型POJO @PathVariable注解 @RequestParam 注解 @RequestBody注解 复杂对象Arra ...