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 【最优比率生成树】的更多相关文章

  1. POJ2728 Desert King —— 最优比率生成树 二分法

    题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Subm ...

  2. POJ2728 Desert King 最优比率生成树

    题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...

  3. POJ 2728 Desert King 最优比率生成树

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20978   Accepted: 5898 [Des ...

  4. 【POJ2728】Desert King 最优比率生成树

    题目大意:给定一个 N 个点的无向完全图,边有两个不同性质的边权,求该无向图的一棵最优比例生成树,使得性质为 A 的边权和比性质为 B 的边权和最小. 题解:要求的答案可以看成是 0-1 分数规划问题 ...

  5. Desert King(最优比率生成树)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22717   Accepted: 6374 Desc ...

  6. POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)

    题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...

  7. POJ 2728 Desert King(最优比率生成树, 01分数规划)

    题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...

  8. POJ 2728 Desert King (最优比率树)

    题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差,现在要求方案使得费用与距离的比值最小,很显然,这个题目 ...

  9. poj-2728Desert King(最优比率生成树)

    David the Great has just become the king of a desert country. To win the respect of his people, he d ...

  10. POJ 2728 Desert King (最优比例生成树)

    POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...

随机推荐

  1. init() 方法

    1 /** * servlet init()在web应用程序启动之后,第一个请求到达Servlet之前调用. * 问题1:如果init()中需要处理大量的工作,那么servlet在处理第一个请求需要花 ...

  2. DML,DML,DCL,DQL

    可以先看看这篇微博:http://blog.csdn.net/jiben2qingshan/article/details/7832344 http://blog.163.com/chenwenlin ...

  3. 树 & 二叉树

    2018-01-04 19:13:46 一.树 在计算机科学中,树(英语:tree)是一种数据结构,用来模拟具有树状结构性质的数据集合.它是由n(n>0)个有限节点组成一个具有层次关系的集合.把 ...

  4. git-----初始化配置添加用户名和密码

    Git是分布式版本控制系统,GitHub 是最大的 Git 版本库托管商,是成千上万的开发者和项目能够合作进行的中心. 大部分 Git 版本库都托管在 GitHub,很多开源项目使用 GitHub 实 ...

  5. 重新学习MySQL数据库2:『浅入浅出』MySQL 和 InnoDB

    重新学习Mysql数据库2:『浅入浅出』MySQL 和 InnoDB 作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.P ...

  6. 2-6-搭建FTP服务器实现文件共享

    ---------------------------------------------- 大纲: FTP服务:(文件传输协议---> File Transfer Protocol) 常见FT ...

  7. 转载--httpclient原理和应用

    https://blog.csdn.net/wangpeng047/article/details/19624529/ 多谢大神的分享

  8. css3中自定义 placeholder 文本颜色

    对于 ie 浏览器我们可以通过自定义的 class 名称,直接修改 span 这个标签的样式.对于其他浏览器诸如谷歌和火狐就需要特殊处理了,不多说直接上代码: ::-webkit-input-plac ...

  9. qt4.8中多线程的几种方式

    第一: 用QtConcurrentRun类,适合在另一个线程中运行一个函数.不用继承类,很方便 第二:用QRunnable和QThreadPool结合.继承QRunnable,重写run函数,然后用Q ...

  10. wordcount程序出现map 100% reduce 0%问题的解决方法

    运行wordcount程序一直停在map 100% reduce 0%, input文件夹的内容: 其中: f1.txt中的内容为:hello hadoop f2.txt中的内容为:hello had ...