POJ2728 Desert King 【最优比率生成树】
POJ2728 Desert King
Description
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
已经确定了一个根,再选择一种优美的结构,不就是树吗(结构是树的时候代价最小),那么这道题就变成一道最优比率生成树,这个题目j就是要求Min(∑升降机成本/∑通道长度)" role="presentation">Min(∑升降机成本/∑通道长度)Min(∑升降机成本/∑通道长度),套一个板子就好啦
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int N=1010;
const double eps=1e-5;
const double INF=1e16;
int n;
double f[N][N],dis[N][N],x[N],y[N],z[N],minv[N];
int vis[N];
double getdis(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;vis[1]=1;
for(int i=1;i<=n;i++)minv[i]=f[1][i]-x*dis[1][i];
for(int i=2;i<=n;i++){
double tmp=INF;int k=-1;
for(int j=2;j<=n;j++)
if(!vis[j]&&minv[j]<tmp)
k=j,tmp=minv[j];
if(k==-1)break;
vis[k]=1;
sum+=tmp;
for(int j=2;j<=n;j++)
if(!vis[j]&&f[k][j]-x*dis[k][j]<minv[j])
minv[j]=f[k][j]-x*dis[k][j];
}
return sum>=0;
}
int main(){
while(1){
scanf("%d",&n);
if(!n)break;
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]=getdis(i,j);
f[i][j]=f[j][i]=abs(z[i]-z[j]);
}
double l=0.0,r=100.0;
while(r-l>=eps){
double mid=(l+r)/2;
if(check(mid))l=mid;
else r=mid;
}
printf("%.3lf\n",r);
}
return 0;
}
POJ2728 Desert King 【最优比率生成树】的更多相关文章
- POJ2728 Desert King —— 最优比率生成树 二分法
题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Subm ...
- POJ2728 Desert King 最优比率生成树
题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- 【POJ2728】Desert King 最优比率生成树
题目大意:给定一个 N 个点的无向完全图,边有两个不同性质的边权,求该无向图的一棵最优比例生成树,使得性质为 A 的边权和比性质为 B 的边权和最小. 题解:要求的答案可以看成是 0-1 分数规划问题 ...
- Desert King(最优比率生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22717 Accepted: 6374 Desc ...
- POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)
题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...
- POJ 2728 Desert King(最优比率生成树, 01分数规划)
题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...
- POJ 2728 Desert King (最优比率树)
题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差,现在要求方案使得费用与距离的比值最小,很显然,这个题目 ...
- poj-2728Desert King(最优比率生成树)
David the Great has just become the king of a desert country. To win the respect of his people, he d ...
- POJ 2728 Desert King (最优比例生成树)
POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...
随机推荐
- 从Github上轻松安装R包—githubinstall包--转载
1.综述 越来越多的R包正在由世界上不同的人所创建,其中一部分原因是devtools包使得开发R包1变得更加简单.devtools包不仅让开发R包变得简单,而且用于分发R包. 当开发者发布一个R包的时 ...
- 转载:Chrome 控制台不完全指南
Chrome的开发者工具已经强大到没朋友的地步了,特别是其功能丰富界面友好的console,使用得当可以有如下功效: 更高「逼格」更快「开发调试」更强「进阶级的Frontender」 Bug无处遁形「 ...
- 利用python 模块读取csv文件信息
还有一个比较简单的方法 # -*- coding=utf-8 -*- import pandas as pddf = pd.read_csv("20170320094630.csv" ...
- FluentData,一个轻量级开源的.NET ORM数据持久化框架
FluentData:一种使用Fluent API的新型轻量级ORM模型 FluentData 是微型 ORM(micro-ORM)家族的一名新成员,旨在比大型 ORM(full ORM)更加易用. ...
- centos7&redhat 之 firewalld 详细介绍配置
firewalld和iptables的关系 firewalld自身并不具备防火墙的功能,而是和iptables一样需要通过内核的netfilter来实现,也就是说firewalld和iptables一 ...
- 虚拟机中安装windows server 2008方法
我们简单的介绍一下怎么在虚拟机上安装 windows server 2008系统. 工具/原料 已经安装好的虚拟机. windows server 2008 iso系统镜像 方法/步骤1虚拟机上虚 ...
- Java 进阶7 并发优化 1 并行程序的设计模式
本章重点介绍的是基于 Java并行程序开发以及优化的方法,对于多核的 CPU,传统的串行程序已经很好的发回了 CPU性能,此时如果想进一步提高程序的性能,就应该使用多线程并行的方式挖掘 CPU的 ...
- C++进阶1模板的使用
C++进阶1模板的使用 20131010 C++中,我们自己编程虽然不会怎么使用模板,包括函数模板和类模板,但是在大型的项目开发中函数模板和类模板是非常重要的.笔者在**面试的时候忙问道过这个问题,迷 ...
- 内存保护机制及绕过方法——利用Ret2Libc绕过DEP之ZwSetInformationProcess函数
1. DEP内存保护机制 1.1 DEP工作原理 分析缓冲区溢出攻击,其根源在于现代计算机对数据和代码没有明确区分这一先天缺陷,就目前来看重新去设计计算机体系结构基本上是不可能的,我们只能靠 ...
- TCP三次握手,四次挥手,状态变迁图
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...