Desert King POJ - 2728(最优比率生成树)

2024-04-11 15:08

本文主要是介绍Desert King POJ - 2728(最优比率生成树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way. 

After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary channels to bring water to all the villages, which means there will be only one way to connect each village to the capital. 

His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that each village is at a different altitude, and different channels can't share a lifter. Channels can intersect safely and no three villages are on the same line. 

As King David's prime scientist and programmer, you are asked to find out the best solution to build the channels.

Input

There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.

Output

For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.

Sample Input

4
0 0 0
0 1 1
1 1 2
1 0 3
0

Sample Output

1.000

题意:

给你一个数字N,代表在沙漠中有N个村庄;后面有N组数据,每个数据三个数字X,Y,Z代表村庄在位置(X,Y),且村庄的海拔是Z;

国王想要给每一个村庄供水,没修建一条路的花费是两个村庄之间的海拔之差;

问你想要使每一个村庄的都连接起来,建成的路的平均每公里的花费最小;

思路:

其实这道题是让你求出来  ai / bi  所有和的最小值;

那么假设 ai / bi >=x,那么ai - x*bi >= 0;

这道题可以使用最优比率生成树算法,其中用二分法来枚举  合适的 x 的值;

令F(x)=ai - x*bi,那么这就可以看成一个一元一次的方程式:F(x)=A - x*B,即F(x)=  - B *x + A;

还是看这个图,用二分来枚举f(x) ;

因为是要求最小生成树,所以在每一次的二分判断中要跑一遍prim算法来判断现在的最小生成树的值是否小于0;

如果小于0,那么就是二分区间大了,缩小区间;

如果大于0,那么就是二分区间小了,扩大区间;

代码如下:

#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;const double inf=1e18;
const int maxn=1e3+10;int n;
double cost[maxn][maxn];
double dis[maxn][maxn];
double x[maxn],y[maxn],z[maxn];
int vis[maxn];double get_dis(int a,int b)///计算两点之间的距离;
{return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}int check(double x)
{memset(vis,0,sizeof vis);double sum=0,lowcost[maxn];vis[1]=1;for(int i=1; i<=n; i++)///计算最小的每一单位距离的花费;lowcost[i]=cost[1][i]-x*dis[1][i];///prim算法;for(int i=2; i<=n; i++)///最小生成树,n-1条边;{double temp=inf;int k=-1;///记录是否还有最小的边;for(int j=2; j<=n; j++){if(!vis[j]&&lowcost[j]<temp){k=j;temp=lowcost[j];}}if(k==-1)break;vis[k]=1;sum+=temp;for(int j=2; j<=n; j++)///松弛;{if(!vis[j]&&cost[k][j]-x*dis[k][j]<lowcost[j])lowcost[j]=cost[k][j]-x*dis[k][j];}}if(sum>=0)return 1;elsereturn 0;
}int main()
{while(~scanf("%d",&n)&&n){for(int i=1; i<=n; i++){scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);}for(int i=1; i<=n; i++)///存储图的数据;{for(int j=i+1; j<=n; j++){dis[i][j]=dis[j][i]=get_dis(i,j);cost[i][j]=cost[j][i]=fabs(z[i]-z[j]);}}///二分枚举;double l=0.0,r=100.0;while(r-l>=1e-5){double mid=(l+r)/2;if(check(mid))l=mid;///扩大区间;elser=mid;///缩小区间;}printf("%.3f\n",l);///注意输出格式;}return 0;
}

 

这篇关于Desert King POJ - 2728(最优比率生成树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/894417

相关文章

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

在java中如何将inputStream对象转换为File对象(不生成本地文件)

《在java中如何将inputStream对象转换为File对象(不生成本地文件)》:本文主要介绍在java中如何将inputStream对象转换为File对象(不生成本地文件),具有很好的参考价... 目录需求说明问题解决总结需求说明在后端中通过POI生成Excel文件流,将输出流(outputStre

C/C++随机数生成的五种方法

《C/C++随机数生成的五种方法》C++作为一种古老的编程语言,其随机数生成的方法已经经历了多次的变革,早期的C++版本使用的是rand()函数和RAND_MAX常量,这种方法虽然简单,但并不总是提供... 目录C/C++ 随机数生成方法1. 使用 rand() 和 srand()2. 使用 <random

Flask 验证码自动生成的实现示例

《Flask验证码自动生成的实现示例》本文主要介绍了Flask验证码自动生成的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 目录生成图片以及结果处理验证码蓝图html页面展示想必验证码大家都有所了解,但是可以自己定义图片验证码