Language:
Default
Desert King
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 22113   Accepted: 6187

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

题意:将n个村庄连在一起,告诉每一个村庄的三维坐标,村庄之间的距离为水平方向上的距离。花费为垂直方向上的高度差。求把村庄连接起来的最小的花费与长度之比为多少。

思路:经典的01分数规划问题,參考这位大神的解说应该就能明确了:http://www.cnblogs.com/Fatedayt/archive/2012/03/05/2380888.html

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <map>
  8. #include <stack>
  9. #include <vector>
  10. #include <set>
  11. #include <queue>
  12. #pragma comment (linker,"/STACK:102400000,102400000")
  13. #define pi acos(-1.0)
  14. #define eps 1e-6
  15. #define lson rt<<1,l,mid
  16. #define rson rt<<1|1,mid+1,r
  17. #define FRE(i,a,b) for(i = a; i <= b; i++)
  18. #define FREE(i,a,b) for(i = a; i >= b; i--)
  19. #define FRL(i,a,b) for(i = a; i < b; i++)
  20. #define FRLL(i,a,b) for(i = a; i > b; i--)
  21. #define mem(t, v) memset ((t) , v, sizeof(t))
  22. #define sf(n) scanf("%d", &n)
  23. #define sff(a,b) scanf("%d %d", &a, &b)
  24. #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
  25. #define pf printf
  26. #define DBG pf("Hi\n")
  27. typedef long long ll;
  28. using namespace std;
  29.  
  30. #define INF 0x3f3f3f3f
  31. #define mod 1000000009
  32. const int maxn = 1005;
  33. const int MAXN = 2005;
  34. const int MAXM = 200010;
  35. const int N = 1005;
  36.  
  37. double x[maxn],y[maxn],z[maxn];
  38. double dist[maxn],mp[maxn][maxn],len[maxn][maxn],cost[maxn][maxn];
  39. bool vis[maxn];
  40. int pre[maxn];
  41. int n;
  42.  
  43. double Dis(int i,int j)
  44. {
  45. return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
  46. }
  47.  
  48. double prim(double r)
  49. {
  50. int i,j,now;
  51. double mi,c=0,l=0;
  52. for (i=0;i<n;i++)
  53. {
  54. dist[i]=INF;
  55. for (j=0;j<n;j++)
  56. {
  57. mp[i][j]=cost[i][j]-r*len[i][j];
  58. }
  59. }
  60. for (i=0;i<n;i++)
  61. {
  62. dist[i]=mp[i][0];
  63. pre[i]=0;
  64. vis[i]=false;
  65. }
  66. dist[0]=0;
  67. vis[0]=true;
  68. for (i=1;i<n;i++)
  69. {
  70. mi=INF;now=-1;
  71. for (j=0;j<n;j++)
  72. {
  73. if (!vis[j]&&mi>dist[j])
  74. {
  75. mi=dist[j];
  76. now=j;
  77. }
  78. }
  79. if (now==-1) break;
  80. vis[now]=true;
  81. c+=cost[pre[now]][now];
  82. l+=len[pre[now]][now];
  83. for (j=0;j<n;j++)
  84. {
  85. if (!vis[j]&&dist[j]>mp[now][j])
  86. {
  87. dist[j]=mp[now][j];
  88. pre[j]=now;
  89. }
  90. }
  91. }
  92. return c/l;
  93. }
  94.  
  95. int main()
  96. {
  97. #ifndef ONLINE_JUDGE
  98. freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
  99. #endif
  100. int i,j;
  101. while (sf(n))
  102. {
  103. if (n==0) break;
  104. for (i=0;i<n;i++)
  105. scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
  106. for (i=0;i<n;i++)
  107. {
  108. for (j=0;j<n;j++)
  109. {
  110. len[i][j]=Dis(i,j);
  111. cost[i][j]=fabs(z[i]-z[j]);
  112. }
  113. }
  114. double r=0,rate; //r迭代初值为0
  115. while (1)
  116. {
  117. rate=r;
  118. r=prim(r);
  119. if (fabs(r-rate)<eps) break;
  120. }
  121. printf("%.3f\n",r);
  122. }
  123. return 0;
  124. }

Desert King (poj 2728 最优比率生成树 0-1分数规划)的更多相关文章

  1. poj 2728 最优比例生成树(01分数规划)模板

    /* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1 ...

  2. [POJ2728] Desert King 解题报告(最优比率生成树)

    题目描述: David the Great has just become the king of a desert country. To win the respect of his people ...

  3. poj 2728 最优比率生成树

    思路:设sum(cost[i])/sum(dis[i])=r;那么要使r最小,也就是minsum(cost[i]-r*dis[i]);那么就以cost[i]-r*dis[i]为边权重新建边.当求和使得 ...

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

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

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

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

  6. Desert King POJ - 2728(最优比率生产树/(二分+生成树))

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

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

    Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS   Memory Limit: 65536K       Descripti ...

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

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

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

    http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...

随机推荐

  1. gocode安装不上的解决办法

    mkdir -p $GOPATH/src/golang.org/x  //路径下创建此文件 cd $GOPATH/src/golang.org/x      //切换到此目录 git clone ht ...

  2. C/C++语言:科学计数法

    主要用来表示浮点数,表达方便 浮点数的科学计数,由三个部分组成: a + E + b a:由一个浮点数组成,如果写成整数,编译器会自动转化为浮点数: E:可以大写E,也可以小写e: b:使用一个十进制 ...

  3. executeFind(XXX) is undefined for the type hibernateTemplate(大概是这个错误吧)

    两句话,jar包版本不一样,类中包含的方法可能有改变. 出错时用的是spring5.x版本,但是没有找到我的api.(不记得放在那里了),所以换了spring的版本(换成了spring3.x).问题解 ...

  4. JavaSE-31 Java正则表达式

    概述 正则表达式是一个强大的字符串处理工具,可以实现对字符串的查找.提取.分割.替换等操作. String类的几个方法需要依赖正则表达式的支持. 方法 方法说明 boolean matches(Str ...

  5. 清除oracle归档日志

    清除oracle归档日志 1. 连接oracle报如下错误 ORA-00257: archiver error. Connect internal only, until freed 产生原因:出现O ...

  6. 函数内部属性之arguments和this

    在函数内部,有两个特殊的对象:arguments和this. 1.arguments arguments是一个类数组对象.包含着传入函数中的所有参数.但这个对象还有一个名叫callee的属性,该属性是 ...

  7. HNOI2006 潘多拉的盒子

    题目描述 题解: 题目的描述比较长,理解起来也有一定难度.仔细读题后我们发现整个任务可以分成两个部分:找出咒语机之间所有的升级关系.求最长升级序列. 1. 求升级关系: 容易看出,咒语机i可以抽象成一 ...

  8. 6. 将单独表空间(File-Per-Table Tablespaces)复制到另一个实例

    6. 将单独表空间复制到另一个实例 本节介绍如何将单独表空间从一个MySQL实例复制 到另一个MySQL实例,也称为可传输表空间功能. 将InnoDB单独表空间复制到其他实例的原因有很多: - 在不对 ...

  9. 【笔记】linux x86漏洞利用

    0x1任意代码执行是如何实现的? 任意代码执行使用一种叫“覆盖返回地址”的技术来实现.这种方式使得攻击者重写位于栈上的返回地址,这将导致任意代码执行.

  10. 大数据学习——linux常用命令(一)

    一.基本日常操作命令 1 查看当前所在工作目录的全路径 pwd 2 查看当前系统的时间 date 设置时间,date -s"2018-11-12" 修改时间后,需要写入硬件bios ...