题目链接:http://poj.org/problem?id=2728

Desert King
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 26878   Accepted: 7459

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

  1. 4
  2. 0 0 0
  3. 0 1 1
  4. 1 1 2
  5. 1 0 3
  6. 0

Sample Output

  1. 1.000

Source

 
 
 
题解:
 
 
代码一:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. #define ms(a,b) memset((a),(b),sizeof((a)))
  13. using namespace std;
  14. typedef long long LL;
  15. const double EPS = 1e-;
  16. const int INF = 2e9;
  17. const LL LNF = 2e18;
  18. const int MAXN = 1e3+;
  19.  
  20. int n;
  21. double x[MAXN], y[MAXN], z[MAXN];
  22. double dis[MAXN][MAXN], height[MAXN][MAXN], d[MAXN][MAXN];
  23. //dis为两点间的距离, height为两点间的高度差。d[i][j] = height[i][j] - L*dis[i][j],作为图的边权。
  24.  
  25. bool vis[MAXN];
  26. double cost[MAXN];
  27. double prim()
  28. {
  29. ms(vis, );
  30. for(int i = ; i<=n; i++)
  31. cost[i] = d[][i];
  32. vis[] = true;
  33.  
  34. double sum = ;
  35. for(int i = ; i<=n-; i++)
  36. {
  37. int k;
  38. double minn = INF;
  39. for(int j = ; j<=n; j++)
  40. if(!vis[j] && minn>cost[j])
  41. minn = cost[k=j];
  42.  
  43. vis[k] = true;
  44. sum += cost[k]; //加上边权
  45. for(int j = ; j<=n; j++)
  46. if(!vis[j])
  47. cost[j] = min(cost[j], d[k][j]);
  48. }
  49. return sum;
  50. }
  51.  
  52. bool test(double L)
  53. {
  54. for(int i = ; i<=n; i++)
  55. for(int j = ; j<=n; j++)
  56. d[i][j] = height[i][j]-L*dis[i][j];
  57.  
  58. return prim()>=;
  59. }
  60.  
  61. int main()
  62. {
  63. while(scanf("%d", &n) && n)
  64. {
  65. for(int i = ; i<=n; i++)
  66. scanf("%lf%lf%lf", &x[i], &y[i], &z[i]);
  67.  
  68. for(int i = ; i<=n; i++)
  69. for(int j = ; j<=n; j++)
  70. {
  71. dis[i][j] = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
  72. height[i][j] = fabs(z[i]-z[j]);
  73. }
  74.  
  75. double l = , r = 100.0;
  76. while(l+EPS<=r)
  77. {
  78. double mid = (l+r)/;
  79. if(test(mid))
  80. l = mid + EPS;
  81. else
  82. r = mid - EPS;
  83. }
  84. printf("%.3f\n", r);
  85. }
  86. }

POJ2728 Desert King —— 最优比率生成树 二分法的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. poj-2728Desert King(最优比率生成树)

    David the Great has just become the king of a desert country. To win the respect of his people, he d ...

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

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

随机推荐

  1. FastDFS上传/下载过程[转载-经典图列]

    FastDFS上传/下载过程: 首先客户端 client 发起对 FastDFS 的文件传输动作,是通过连接到某一台 Tracker Server 的指定端口来实现的,Tracker Server 根 ...

  2. HTML网页滚动加载--mark一下

    console控制台: >: function stroll(){ window.scrollTo(, document.body.scrollHeight); }; >: window. ...

  3. CentOS 7.3 源码安装 OpenVAS 9

    https://my.oschina.net/u/2613235/blog/1583198

  4. android调用 .net webService

    package com.rockcheck.mes; import android.os.AsyncTask; import android.support.v7.app.AppCompatActiv ...

  5. CDOJ_844 程序设计竞赛

    原题地址:http://acm.uestc.edu.cn/#/problem/show/844 "你动规无力,图论不稳,数据结构松散,贪心迟钝,没一样像样的,就你还想和我同台竞技,做你的美梦 ...

  6. delphi学习路线

     酷派(53376063) 11:04:19 1.语法基础PASCAL精要 先看个1-3遍,这是基础中的基础,只要没弄清楚看10遍都不多,当然最好结合着代码实例去看.(以后遇到哪儿不熟练继续反复看)2 ...

  7. Maven使用site-deploy(site:deploy)部署通过site生成的文档(Tomcat-WebDAV)

    Maven可以通过site生成项目的帮助文档,并且格式为html,那么可以通过site-deploy把文档部署到远端,部署方式支持HTTP/FTP/SCM/WebDAV等. 更多部署方案,参考:htt ...

  8. UItableView 所有内容保存为图片

    将所有的UITableView保存为图片,因为UITableView只能保存显示当前,所以,就单个保存后,合并为一张图片 代码如下: -(IBAction)savePic:(id)sender { / ...

  9. IronPython 与C#交互

    http://www.cnblogs.com/nuaalfm/archive/2010/02/11/1667448.html 一.介绍 Python是一种面向对象.直译式计算机程序设计语言,也是一种功 ...

  10. 使用RPi-Monitor监控、统计Guitar的运行状态

    前言 之前发在ickey社区上的一系列文章: 犹抱琵琶半遮面,无人知是荔枝来--unboxing & interview 一.二.三 葡萄美酒夜光杯,巧妇难为无米炊--资料与社区 一支穿云箭, ...