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}} \] 其中 ...
随机推荐
- Mac系统下VirtualBox装Centos7设置静态IP并连网
用Virtualbox装了三台Centos7,现在需要设置成三台之间可以相互通信,并且三台都可以连外网. 需求如下: 1. 三台内部相互通信 2. 可以上外网 3. 主机可以虚拟机可以相互通信(she ...
- python 数据库操作产生中文乱码的解决办法
1.执行python mysql数据库查询操作时,产生中文乱码 #!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb db = MySQLd ...
- Leetcode 274.H指数
H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...
- 【Ts 2】Nginx服务器搭建
在项目中,首先是需要Nginx服务器作为一个图片服务器来使用.那么,久涉及到服务器的搭建.这次服务器的搭建,主要是在三个环境上进行了学习:CentOS6.2,CentOS7,和Ubuntu16.那么本 ...
- LibreOJ2302 - 「NOI2017」整数
Portal Description 有一个整数\(x=0\),对其进行\(n(n\leq10^6)\)次操作: 给出\(a(|a|\leq10^9),b(b\leq30n)\),将\(x\)加上\( ...
- Codeforces889C. Maximum Element
$n \leq 2000000$的排列,问有多少满足:存在个$i$,使得$p_i \neq n$,且$p_j<p_i,j \in [i+1,i+K]$,$K \leq 2000000$是给定常数 ...
- zookeeper学习0
参考文献: 5分钟让你了解 ZooKeeper 的功能和原理 Zookeeper专题——1.分布式事务(a概述) Zookeeper专题——2.分布式锁-基于Zookeeper的分布式锁
- 洛谷——P2820 局域网
P2820 局域网 题目背景 某个局域网内有n(n<=100)台计算机,由于搭建局域网时工作人员的疏忽,现在局域网内的连接形成了回路,我们知道如果局域网形成回路那么数据将不停的在回路内传输,造成 ...
- Java反射常用示例
package xmq.study.reflection; import java.lang.annotation.Annotation; import java.lang.reflect.Const ...
- 报错: The type ByteInputStream is not accessible due to restriction on required library
报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...