题干:

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

题目大意:

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

一句话题意:

解题报告:

除了二分,还有一种算法叫Dinkelbach算法

每次将r'代入z函数中计算以后,我们将得到一组x

让r''=(∑(ci*xi))/(∑(di*xi))

当r''=r'时,r''就是我们需要的解

否则将r'=r'',继续迭代

这种方法比二分法要快一点

AC代码:(迭代法)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n;
const double eps = 1e-5;
struct Node {double x,y,z;
} node[MAX];
struct NN {int u,v;double x,y;
} ee[20 * MAX];
struct Edge {int u,v;double x,y,w;
} e[20 * MAX];
int f[MAX];
int tot;
bool cmp(Edge a,Edge b) {return a.w < b.w;
}
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);f[t2] = t1;
}
void init() {for(int i = 1; i<=n; i++) f[i] = i;
}
double kls() {sort(e+1,e+tot+1,cmp);init();int cnt = 0;double xx = 0,yy = 0;for(int i = 1; i<=tot; i++) {if(getf(e[i].u) != getf(e[i].v)) {xx += e[i].x;yy += e[i].y;cnt ++;merge(e[i].u,e[i].v);if(cnt == n - 1) break;}}return xx/yy;
}
double ok(double mid) {for(int i = 1; i<=tot; i++) e[i].u = ee[i].u,e[i].v = ee[i].v,e[i].x = ee[i].x,e[i].y = ee[i].y,e[i].w = ee[i].x - mid*ee[i].y;double res = kls();return res;
}
int main()
{while(~scanf("%d",&n)) {if(n == 0) break;tot = 0;//init() for(int i = 1; i<=n; i++) f[i] = i;for(int i = 1; i<=n; i++) {scanf("%lf%lf%lf",&node[i].x,&node[i].y,&node[i].z);}for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {ee[++tot].y = sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x) + (node[i].y-node[j].y)*(node[i].y-node[j].y));ee[tot].x = fabs(node[i].z - node[j].z);ee[tot].u = i;ee[tot].v = j;}    }double l = 0,r = 1e9;
//      double mid = (l+r)/2,ans=mid;
//      while(l+eps < r) {
//          mid = (l+r)/2;
//          if(ok(mid)) r = mid;
//          else l = mid;
//      }double mid = (l+r)/2,ans = -1;while(fabs(ans-mid) >= eps) {ans = mid;mid = ok(mid);}printf("%.3f\n",ans - eps);}return 0 ;}

TLE代码:(二分)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n;
const double eps = 1e-5;
struct Node {double x,y,z;
} node[MAX];
struct NN {int u,v;double x,y;
} ee[20 * MAX];
struct Edge {int u,v;double w;
} e[20 * MAX];
int f[MAX];
int tot;
bool cmp(Edge a,Edge b) {return a.w < b.w;
}
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);f[t2] = t1;
}
void init() {for(int i = 1; i<=n; i++) f[i] = i;
}
double kls() {sort(e+1,e+tot+1,cmp);init();int cnt = 0;double res = 0;for(int i = 1; i<=tot; i++) {if(getf(e[i].u) != getf(e[i].v)) {res += e[i].w;cnt ++;merge(e[i].u,e[i].v);if(cnt == n - 1) break;}}return res;
}
bool ok(double mid) {for(int i = 1; i<=tot; i++) e[i].u = ee[i].u,e[i].v = ee[i].v,e[i].w = ee[i].x - mid*ee[i].y;double res = kls();if(res <=0) return 1;else return 0 ;
}
int main()
{while(~scanf("%d",&n)) {if(n == 0) break;tot = 0;init();for(int i = 1; i<=n; i++) {scanf("%lf%lf%lf",&node[i].x,&node[i].y,&node[i].z);}for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {ee[++tot].y = sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x) + (node[i].y-node[j].y)*(node[i].y-node[j].y));ee[tot].x = fabs(node[i].z - node[j].z);ee[tot].u = i;ee[tot].v = j;} }double l = 0,r = 1e9;double mid = (l+r)/2;while(l+eps < r) {mid = (l+r)/2;if(ok(mid)) r = mid;else l = mid;}printf("%.3f\n",mid - eps);}return 0 ;}

AC代码:

【POJ - 2728】Desert King (最有比率生成树,分数规划)相关推荐

  1. poj 2728 Desert King(最小比率生成树 / 0-1分数规划 / 二分)

    二分答案,我们要找最小的答案,如果有更小的答案说明 ∑W−Z∗∑L<=0∑W−Z∗∑L <= 0∑W−Z∗∑L<=0. #include<cstdio> #include ...

  2. POJ 2728 Desert King [最优比率生成树]

    RT 我想哭 我想哭 我想哭 我想哭 我想哭 我想哭 我想哭 我想哭 我想哭 我想哭 我想哭 我想哭 凭什么!一模一样的代码一个TLE一个AC,改小二分范围和精度才过 凭什么! 我眼睁睁的看着那段代码 ...

  3. POJ-2728 Desert King 最优比例生成树 01分数规划/参数搜索

    题意:给定N个三维平面点,每个点都有一个高度,每两个点之间的需要构边,边的距离由x,y坐标的欧几里得距离确定,边的花费有点的高度差即z值确定,现在问一个合理的生成树中,花费比上距离的最小值为多少? 解 ...

  4. 转载二分 01 分数规划即最大化平均值的证明0/1分数规划、最优比率生成树、最优比率环

    首页 新随笔 联系 管理 订阅 随笔- 20  文章- 0  评论- 9 [Algorithm]01分数规划--Update:2012年7月27日 [关键字] 0/1分数规划.最优比率生成树.最优比率 ...

  5. POJ 2728 最优比率生成树

    题意:      让你求一颗最小比率生成树. 思路:      我总结过了,在这里:http://blog.csdn.net/u013761036/article/details/26666261   ...

  6. poj 2728(最小比率生成树)

    参考题解:http://www.cppblog.com/jh818012/articles/167743.html 题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条 ...

  7. Desert King POJ - 2728

    题意: 给定N个平面上的点的坐标和它们的权值,任意两点之间的边的价值是它们的距离,费用是两点权值之差的绝对值,求该图的一棵生成树,使得该树所有边的费用之和与价值之和的比值最小(只需求这个比值即可) 题 ...

  8. poj2728(最优比率生成树)

    1.最优比率生成树(最优比例生成树): 定义:有向带权图G, 对于图中每条边e[i], 都有cost[i]权值)和dist[i](距离), 要求的是一棵生成树T, 它使得 ∑(cost[i]) / ∑ ...

  9. POJ 2728 01分数规划

    题意: 最优比率生成树,要求生成树中的所有边的花费与所有边的长度的比值最小 题解: 01分数规划,详见http://www.cnblogs.com/proverbs/archive/2013/01/0 ...

最新文章

  1. Silverlight入门:第三部分 - 数据访问
  2. 2. JSF---托管Bean
  3. matlab unique函数
  4. oracle生成42位,Oracle HowTo:如何确定Oracle是32 Bit(位)的还是64 Bit(位)的?
  5. 【greenplum】 gp的安装
  6. python 自动输入_Python自动输入【新手必学】
  7. export mysql home_mysql的Linux下安装笔记
  8. java中final注意的问题
  9. 前端技术基础(一):浏览器相关
  10. 怎样从本地删除git远程仓库里面的文件
  11. 九存:重新定义存储矿机
  12. 学校计算机学院教学管理ER图,学校课程管理ER图
  13. sudo yum install glibc.i686 linux,yum安装glibc-devel.i686的问题经历
  14. 2015 年度新增开源软件排名 TOP100
  15. C语言零碎知识点之输入字符数组
  16. 【CANdelaStudio编辑CDD】-0.1-如何对比两个CDD诊断描述文件
  17. Swift 下标用法
  18. 判断一个数是否为质数的三种方法
  19. python not found in axis_关于python:pandas-drop函数错误 (label not contained in axis)
  20. 走方格跳格子(dp,递归,排列组合三种方法)

热门文章

  1. 推荐笔记本用户使用的硬盘。。。2.5”战胜台式机硬盘 日立7K320-250GB
  2. 动态规划——背包问题升级
  3. python爬取百度贴吧中的所有邮箱_python写的百度贴吧邮箱采集(带界面)
  4. 和县机电工程学校工业机器人_【校企合作】学校举行工业机器人教学系统捐赠仪式...
  5. android实现3种定位的切换,Android 滑动定位+吸附悬停效果实现
  6. datetime mysql 当天_MySQL 获得当前日期时间(以及时间的转换)
  7. shell设计精髓_交互设计精髓
  8. js urlencode 20 php,js实现php函数urlencode
  9. Spring webflow:上传多个文件
  10. 毕业论文排版之Word 中公式居中,编号靠右该怎么设置(针对左右不对称页边距)