题目描述:

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

题意:有一张无向图,每条边有费用值和长度值,要求找到一颗生成树,使得总费用/总长度最小
问题模型:最优比率生成树
解题方法:
设r=Σc[i]*x[i]/(Σl[i]*x[i])(x[i]=0/1)
目标,最小化r
变形得:Σc[i]*x[i]-r*Σl[i]*x[i]=0
设f[r]=Σc[i]*x[i]-r*Σl[i]*x[i]于是对于每一组x[i],以我们得到一条直线,r是这条直线的横截距(确定x[i]之后其它的都是常数)我们的目标变成,找到全部直线中的最小横截距怎么找呢?首先暴力枚举x[i]绝对是不可取的。我们发现其实只需要二分r的值,然后观察min(f[r])就好,如果min(f[r])还要小于0的话,我们就可以知道r>r*(r*是我们最后的所求值),反之r<r*当我们我们发现min(f[r])==0时,我们认为r=r*然后就直接输出考虑一下二分的时间复杂度,完全没有问题(注意这里是最大化r,包括下面的图,最小化是同理的)


(图片来自大佬ztx的CSDN博客,https://blog.csdn.net/hzoi_ztx/article/details/54898323,感谢大佬)
然而,我们还有一种方法(DinkelbachDinkelbach算法)
基本思想,其实和二分有点像,但它是基于迭代的。我们考虑如上述二分,我们有一个初始值r,
然后我们发现min(f[r])还要小于0,
那我们就直接把r转移到min(f[r])所代表的直线的横截距上。读者细细想想
就知道,不可能得到一个比r*还要小的值,因为r*就是最小的截距了。
于是我们只需要不断迭代转移r就好了。 咳咳,差点忘了。怎么求min(f[r]),其实很显然,为了得到min(f[r])的x[i],
我们得把边的长度按照上面f函数的计算方法改一改跑最小生成树。
为什么呢?
首先这样我们肯定可以得到一组x[i]
同时最小就是保证了我们要算的f的取值最小
代码里是堆优化的prim(我懒。。。不手写堆的)
下面附上我的DinkelbachDinkelbach代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<memory.h>
#include<queue>
#include<math.h>
using namespace std; const int maxn=1000+15;
int n,tot;
int head[maxn],in[maxn];
struct VILLAGE
{
double x;double y;double z;
}v[maxn];
struct EDGE
{
int from;int to;int next;double len;double cost;double e;
}edge[maxn<<12];
struct NODE{
int x;double l;double c;double d;
};
bool operator < (const NODE a,const NODE b){
return a.l>b.l;
}
void init(){
memset(edge,0,sizeof(edge));
memset(head,0,sizeof(head));
tot=0;
}
void add(int x,int y,double len,double cost){
edge[++tot]=(EDGE){x,y,head[x],len,cost,0};
head[x]=tot;
}
double prim(double oo)
{
memset(in,0,sizeof(in));
for (int i=1;i<=tot;i++)
{
edge[i].e=edge[i].cost-oo*edge[i].len;
//printf("%d %d %lf\n",edge[i].from,edge[i].to,edge[i].e);
}
priority_queue<NODE> q;
q.push((NODE){1,0,0,0});
int cnt=0;
double c1=0,d1=0;
while (!q.empty()&&cnt<n)
{
NODE k=q.top();q.pop();
if (in[k.x]) continue;
in[k.x]=1;
c1+=k.c;d1+=k.d;cnt++;
for (int i=head[k.x];i;i=edge[i].next)
{
int y=edge[i].to;
q.push((NODE){y,edge[i].e,edge[i].cost,edge[i].len});
}
}
//printf("\n%lf\n",c1/d1);
return c1/d1;
}
int main()
{
while (1)
{
scanf("%d",&n);
if (n==0) break;
init();
for (int i=1;i<=n;i++)
scanf("%lf%lf%lf",&v[i].x,&v[i].y,&v[i].z);
for (int i=1;i<n;i++)
for (int j=i+1;j<=n;j++)
{
double dis=sqrt((v[i].x-v[j].x)*(v[i].x-v[j].x)+(v[i].y-v[j].y)*(v[i].y-v[j].y));
double cost=fabs(v[i].z-v[j].z);
add(i,j,dis,cost);
add(j,i,dis,cost);
}
double r1=0.0,r2=0.0;
while (1){
r2=prim(r1);
if (fabs(r2-r1)<0.00001) break;
r1=r2;
}
printf("%.3f\n",r1);
}
return 0;
}

  解决了最小的问题,最大的问题不也就迎刃而解了,只需要找最大截距就好了。这是0/1分数规划的典型之一。

 其他的还有最优比率环什么的。
												

[POJ2728] Desert King 解题报告(最优比率生成树)的更多相关文章

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

    POJ2728 Desert King Description David the Great has just become the king of a desert country. To win ...

  2. 【最优比率生成树】poj2728 Desert King

    最优比率生成树教程见http://blog.csdn.net/sdj222555/article/details/7490797 个人觉得很明白易懂,但他写的代码略囧. 模板题,但是必须Prim,不能 ...

  3. poj2728 Desert King(最小生成树+01分数规划=最优比率生成树)

    题意 n个点完全图,每个边有两个权值,求分数规划要求的东西的最小值. (n<=1000) 题解 心态炸了. 堆优化primT了. 普通的就过了. 我再也不写prim了!!!! 咳咳 最优比率生成 ...

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

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

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

    Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS   Memory Limit: 65536K       Descripti ...

  6. POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)

    [题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...

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

    http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...

  8. 最优比率生成树 poj2728

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28407   Accepted: 7863 Desc ...

  9. [转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环

    01分数规划 前置技能 二分思想最短路算法一些数学脑细胞? 问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此 ...

随机推荐

  1. AIX查看某个端口被哪个进程占用

    AIX查看某个端口被哪个进程占用 学习了:https://zhidao.baidu.com/question/1928716757722021467.html 1. netstat -Aan|grep ...

  2. 【iOS开发-54】案例学习:通过UIScrollView的缩放图片功能练习代理模式的详细实现

    案例:(在模拟器中按住option键,点击鼠标就会出现缩放的手势) (1)在ViewController.m中: --缩放东西是UIScrollView除了滚动之外的还有一个功能,所以须要缩放的东西应 ...

  3. nodejs中require的路径是一个文件夹时发生了什么

    node中使用require的时候如果路径是一个文件夹时,或者特殊的情况require('..');require('.'); 这是node实战这本书里说的情况,但是我在node6.9版本中发现不完全 ...

  4. zzulioj--1712--神秘的数列(水题)

    1712: 神密的数列 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 122  Solved: 92 SubmitStatusWeb Board De ...

  5. ORA-01261: Parameter db_recovery_file_dest destination string cannot be translat

    查看了Oracle的初始化文件. $cat /oracle/oms/102_64/dbs/initSID.ora 如:db_recovery_file_dest='/oracle/oms/flash_ ...

  6. [雅礼NOIP2018集训 day3]

    考试的时候刚了T1两个小时线段树写了三个子任务结果发现看错了题目,于是接下来一个半小时我自闭了 result=历史新低 这告诉我们,打暴力要端正态度,尤其是在发现自己之前出锅的情况下要保持心态的平和, ...

  7. Android 常用系统服务

    WindowManager:WindowManager服务是全局的唯一的.它会将用户在屏幕上的操作发送给界面上的各个Window,Activity会将顶层控件注册到WindowManager中.Win ...

  8. label标签的可访问性问题

    label标签可以优雅地扩大表单控件元素的点击区域.例如,单纯的单选框点击区域就鼻屎那么大的地方,经常会点不到位置.因此,label标签的使用对于提高页面的可用性可访问性是很有帮助的. 其实,它的样子 ...

  9. Codeforces 987C. Three displays(o(n^2))

    刚开始三重循环tle test11.后来想了个双重循环的方法. 解题思路: 1.双重循环一次,用一个一位数组存j和比j小的i的和的最小值. 2.再双重循环一次,找到比j大的数k,更新结果为ans=mi ...

  10. Android程序能够构建和运行,但是报以下报错,为什么?

    安卓程序写完之后能够构建和运行,但是会报以下的错误.不知道原因为何?求大神解答. 网上说的是混淆编译的原因,不过程序没有开启混淆编译. Error:warning: Ignoring InnerCla ...