题目描述:

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. [CSS3] All abourt responsive image

    Take few examples: Full size image: The problem for that is it overflow when the screen size is smal ...

  2. FreeRTOS系列第13篇---FreeRTOS内核控制

    内核控制的一些功能须要移植层提供,为了方便移植.这些API函数用宏来实现,比方上下文切换.进入和退出临界区.禁止和使能可屏蔽中断.内核控制函数还包含启动和停止调度器.挂起和恢复调度器以及用于低功耗模式 ...

  3. 关于sql中的with(nolock)

    SQL Server 中的 NOLOCK 究竟是什么意思 一般用于此类语句中:select * from t with(NOLOCK) nolock是不加锁查询.能够读取被事务锁定的数据,也称为脏读. ...

  4. 一些.NET 项目中经常使用的类库

    Web自己主动化測试   Watin Selenium  Selenium git .net 集合类扩展实现C5 Subscriber/Publisher 模式 Rx Nats 防御式编程 断言库 流 ...

  5. Python学习之基本概念

    1.Python是一种解释型语言.Python解释器通过“一次执行一条语句”的方式执行程序的. 2.Python用空白来组织程序,不像R等用大括号. 3.# 是Python的注释符号. 4.变量是按引 ...

  6. Linux换行符相关

    Linux和windows中的换行符差异问题LINUX的换行符在Windows记事本打开不换行或出现黑点是由于Linux和windows中的换行符差异问题造成的. Unix系统里,每行结尾只有“< ...

  7. GetExecutingAssembly() 和 GetCallingAssembly() 的区别

    在TCX_1710项目代码的启动项目根目录路径下的Global.asax.cs配置文件中的MVCApplication类中的Application_Start()方法中,配置了项目启动时要加载的项目信 ...

  8. dl learn task

    https://deeplearning4j.org/cn/word2vec Task 1 分类http://blog.csdn.net/czs1130/article/details/7071734 ...

  9. 学习参考《父与子的编程之旅python【第二版】》高清中文版PDF+高清英文版PDF+源代码

    对于初步接触编程语言的朋友,推荐看一看<父与子的编程之旅第2版>,对于完全编程零基础的很友好! 图文并茂,过多的文字堆垒很容易让人产生厌倦情绪,也更容易让人产生放弃的想法.使用了大量插图, ...

  10. mysql 修改默认的引擎

      需求: mysql 的默认的引擎为MyISAM  虽然该引擎访问的速度快,但并不支持存储事物,也不支持外键,所以我们修改为innob Linux修改MySql默认存储引擎为InnoDB     一 ...