Desert King
Time Limit: 3000MS   Memory Limit: 65536K
     

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

Source

 
题目大意:构建最优比率生成树
每条边的花费=连接两点的高度差,距离=平面两点间距离(官方名称:欧几里得距离)
最小化 ∑ 每条边的花费/距离
01分数规划+prim
prim构造最小生成树的标准是 w=这条边的花费-这条边的距离*二分的mid
最后判断选用边的w是否大于0
01分数规划就用在这里
然而初学,并没有想到,一直在思考怎么在prim过程中套01规划
再就是判断式子>0,移动下界,否则移动上界
这是固定的,与最终求最大最小值没有关系
受了刚做的一道01规划题影响http://www.cnblogs.com/TheRoadToTheGold/p/6546981.html
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define eps 1e-6
using namespace std;
int n;
double l,r,mid,ans,p;
double x[],y[],h[],dis[][],cost[][],minn[],w[][];
bool v[];
bool check(double k)
{
p=;
memset(v,,sizeof(v));
for(int i=;i<=n;i++) minn[i]=w[][i];
minn[]=;v[]=true;
int s=n-;
while(s--)
{
int point;double d=;
for(int i=;i<=n;i++)
if(!v[i]&&minn[i]<d)
{
point=i;d=minn[i];
}
p+=d;v[point]=true;
for(int i=;i<=n;i++)
if(!v[i]&&w[point][i]<minn[i])
minn[i]=w[point][i];
}
return p>=;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(!n) return ;
for(int i=;i<=n;i++) scanf("%lf%lf%lf",&x[i],&y[i],&h[i]);
for(int i=;i<n;i++)
for(int j=i+;j<=n;j++)
{
dis[i][j]=sqrt(pow(abs(x[i]-x[j]),)+pow(abs(y[i]-y[j]),));
cost[i][j]=abs(h[i]-h[j]);
}
l=,r=;ans=;
while(fabs(l-r)>eps)
{
mid=(l+r)/;
for(int i=;i<n;i++)
for(int j=i+;j<=n;j++)
w[i][j]=w[j][i]=cost[i][j]-mid*dis[i][j];
if(check(mid))
{
ans=mid;
l=mid+eps;
}
else r=mid-eps;
}
printf("%.3lf\n",ans);
} }

poj 2728 Desert King (最优比率生成树)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 使用百度地图api可视化聚类结果

    1.写在前面 上接YFCC 100M数据集分析笔记,在对聚类出的照片GEO集聚类后,为了方便检测聚类结果,我们显示直接采用了 python 的 matplotlib 库以经纬度为坐标画出聚类结果,但发 ...

  2. 小学四则运算练习(JAVA编写)

    源码在Github的仓库主页链接地址:https://github.com/rucr9/rucr 看到这个题目,大概很多人会发出“切,这也太简单了吧!有必要小题大做?”的感叹!是的,仅仅作为一道数学运 ...

  3. HDU 2123 An easy problem

    http://acm.hdu.edu.cn/showproblem.php?pid=2123 Problem Description In this problem you need to make ...

  4. Windows 10 正式版原版ISO镜像

    Win10正式版32位简体中文版(含家庭版.专业版)文件名: cn_windows_10_multiple_editions_x86_dvd_6846431.isoSHA1:21B824F402927 ...

  5. sublinme 快捷键格式

    {"keys": ["ctrl+shift+f"], "command": "reindent" , "arg ...

  6. Java 文件下载功能 解决中文乱码

    Html部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  7. UVAlive3211_Now or later

    白书上的例题. 每种航班可以选择两种时间降落,如果想任意航班降落时间差的最小值最大,应该如何安排? 二分时间,如果两个时间只差小于当前枚举的时间,说明这条边不可选,可以根据2sat的方法构图. 然后判 ...

  8. 机器学习经典论文/survey合集

    Active Learning Two Faces of Active Learning, Dasgupta, 2011 Active Learning Literature Survey, Sett ...

  9. Bob Waters - Twenty Years

    We were just children and grown up closeHow we made it this far only god knowsWe bend the rulesSmash ...

  10. BibTex相关

    标签(空格分隔): 杂七杂八的问题 又到了写论文的高峰期(?)在BibTeX中添加参考文献时,发现选项很多,对一些称呼还是一脸懵逼..阿一古,也许是最后一次写论文了,还弄清楚的还是清楚一下吧~ [转自 ...