【分数规划】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
时间: 2024-10-08 03:46:12

【分数规划】POJ2728 Desert King的相关文章

poj2728 Desert King --- 01分数规划 二分水果。。

这题数据量较大,普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; #define N 1010 double mp[N][N],c[N][N],x

01分数规划+prim POJ2728 Desert King

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 26009   Accepted: 7219 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 coun

poj2728 Desert King——01分数规划

题目:http://poj.org/problem?id=2728 第一道01分数规划题!(其实也蛮简单的) 这题也可以用迭代做(但是不会),这里用了二分: 由于比较裸,不作过多说明了. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #define eps 1e-6 using namespace std; int const inf=0x3f3f3f;

01分数规划poj2728(最优比例生成树)

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21766   Accepted: 6087 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 coun

poj2728 Desert King,最优比例生成树

题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可, 建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差 现在要求方案使得费用与距离的比值最小 很显然,这个题目是要求一棵最优比率生成树, 0-1分数规划,0-1分数规划是分数规划的一种特殊情况,分数规划适用于求解最优化问题的,对于求最大的对应解,该理论也有效 这是从网上找到的具体的最优比率生成树的方法的讲解 //////////////////// 概念 有带权图G, 对于图中每

POJ2728 Desert King

Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu 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

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

最优比率生成树教程见http://blog.csdn.net/sdj222555/article/details/7490797 个人觉得很明白易懂,但他写的代码略囧. 模板题,但是必须Prim,不能用Kruscal,因为是完全图 Code: 1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 struc

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

01分数规划 前置技能 二分思想最短路算法一些数学脑细胞?问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此二元组获得的价值(非负),costicosti是选择此二元组付出的代价(非负),设xi(xi∈{0,1})xi(xi∈{0,1})代表第ii个二元组的选与不选,最大(小)化下式 maximize(or minimize)   r=∑valuei?xi∑costi?ximaximize(or minim

Desert King POJ2728(Prim+迭代+0-1分数规划)

[原题地址]: POJ2728 [题目大意]: 选 \(n-1\) 条边,最小化这些边的\[\frac{\sum w_i} {\sum d_i}\] 其中 \(w_i\) 为第 \(i\) 条边的花费,\(d_i\) 为这条边所连接的两个点的距离. \(w_i\) 为连接的两个点的 \(z\) 值差的绝对值, \(d_i\) 为欧几里得距离 [题解]: 这道题是0-1分数规划的经典题目 我们令\[\frac{\sum w_i} {\sum d_i}=k\] 这样二分 \(k\) 的值 将所有的边