题目描述:

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. ZOJ 2836

    求不比M大的可以被集合任一个数整除的数的个数.(容斥原理) #include <iostream> #include <cstdio> #include <algorit ...

  2. Android性能优化之ListView缓存机制

    要想优化ListView首先要了解它的工作原理,列表的显示须要三个元素:ListView.Adapter.显示的数据. 这里的Adapter就是用到了适配器模式,无论传入的是什么View在ListVi ...

  3. Hadoop实战:使用Combiner提高Map/Reduce程序效率

    好不easy算法搞定了.小数据測试也得到了非常好的结果,但是扔到进群上.挂上大数据就挂了.无休止的reduce不会结束了. .. .. .... .. ... .. ================= ...

  4. ORA-1157错误解决

    一.错误描述 ORA-1157, "cannot identify/lock data file %s - see DBWR trace file" 引起的原因: 因为数据文件已经 ...

  5. 92.bower 需要git

    转自:https://blog.csdn.net/chenleismr/article/details/50458496Bower 是基于 Git 之上的包管理工具,它提供的包其源头都是一个 Git ...

  6. BZOJ 1898 构造矩阵+矩阵快速幂

    思路: T的最小公倍数是12 那么12以内暴力 整除12 的部分用矩阵快速幂 //By SiriusRen #include <cstdio> #include <cstring&g ...

  7. 头像文件上传 方法一:from表单 方法二:ajax

    方法一:from表单 html 设置form表单,内包含头像预览div,内包含上传文件input 设置iframe用来调用函数传参路径 <!--表单提交成功后不跳转处理页面,而是将处理数据返回给 ...

  8. jquery简介 each遍历 prop attr

    一.JQ简介 jQuery是一个快速.简洁的JavaScript框架,它封装了JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作.事件处理.动画设计和 ...

  9. Dapper丶DapperExtention,以及AbpDapper之间的关系,

    在一些开发上,由于系统的迭代上面,不能立即使用ABP框架一些框架,框架是死的,框架是死的,框架中的一些东西很值得我们学习.例如DDD架构设计,Dto数据传输对象,以及AutoMapper.仓储,依赖注 ...

  10. TP5 分页类,自定义样式

    结合X-admin 后台框架 在做项目,为了保持分页风格的一致,所以需要自定义 一个分页类. 一.在项目的 extend 目录,创建 cus 目录 二.创建 Page 分页类,代码如下 <?ph ...