POJ 2728 Desert King
来源:互联网 发布:推荐淘宝上燕窝 编辑:程序博客网 时间:2024/05/28 19:23
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
经典的01分数规划:最优比率生成树问题。
设题目所求的比率为r=ΣC/ΣV,其中v表示价值,c表示费用。
Σc-Σv*r=0。
注意到这个式子可以写成Σ(c-v*r)=0。
于是可以二分答案r,对每个r求以c-v*r为边权的最小生成树,若答案大于0,说明这个r不可行,若答案小于0,说明答案可行,但还有更优解。
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;const int maxn=1005;int n,m;double xx[maxn],yy[maxn],zz[maxn],dis[maxn][maxn],cst[maxn][maxn],d[maxn];bool b[maxn];double prim(double lim){ memset(b,0,sizeof(b)); double sum=0; for(int i=2;i<=n;i++) d[i]=cst[1][i]-lim*dis[1][i]; b[1]=1; int k; for(int i=1;i<=n-1;++i) { double mn=1e9+7; for(int j=1;j<=n;j++) if(!b[j]&&d[j]<mn) { mn=d[j]; k=j; } b[k]=1; sum+=mn; for(int j=1;j<=n;j++) if(!b[j]&&cst[k][j]-lim*dis[k][j]<d[j]) d[j]=cst[k][j]-lim*dis[k][j]; } return sum;}int main(){ while(scanf("%d",&n)&&n) { for(int i=1;i<=n;i++) scanf("%lf%lf%lf",xx+i,yy+i,zz+i); for(int i=1;i<=n;i++) for(int j=1;j<=n;++j) { dis[i][j]=sqrt((xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j])); cst[i][j]=fabs(zz[i]-zz[j]); } double l=0,r=100000,mid,tmp; while(r-l>1e-4) { mid=(l+r)/2; tmp=prim(mid); if(tmp>=0) l=mid; else r=mid; } printf("%.3f\n",l); } return 0;}
- POJ-2728-Desert King
- poj 2728 Desert King
- POJ-2728-Desert King
- poj 2728 Desert King
- POJ 2728 Desert King
- POJ 2728 Desert King
- [POJ 2728]Desert King
- 【POJ 2728】Desert King
- POJ 2728 Desert King
- POJ 2728 Desert King
- POJ 2728 Desert King
- POJ 2728Desert King
- POJ 2728 Desert King
- POJ 2728Desert King
- POJ 2728:Desert King
- POJ 2728 (Desert King)
- 【POJ 2728】[01分数规划]Desert King
- 【POJ】2728 Desert King 分数规划
- iOS开发常用尺寸整理
- 优秀的Java程序测试是什么样的?
- Code=3000 "未找到应用程序的“aps-environment”的授权字符串" 解决办法
- 写一个函数返回参数二进制中1的个数
- 两个栈实现一个队列
- POJ 2728 Desert King
- PLSQL学习——控制语句
- Struts2.0学习笔记---上传文件和多文件上传
- 【软件工程】--文档总结
- 第三周项目2—建设“顺序表”算法库
- 神经网络入门资料
- 蓄水池抽样-Random Pick Index
- C++字符输入
- mac下安装chromedriver