首页 > 代码库 > POJ 2728(最优比率生成树+01规划)
POJ 2728(最优比率生成树+01规划)
Desert King
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 25729 | Accepted: 7143 |
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.
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
40 0 00 1 11 1 21 0 30
Sample Output
1.000
Source
Beijing 2005
先贴个代码 明天找时间来补解释
#include<iostream>#include<cstdio>#include<cstdlib>#include<cctype>#include<cmath>#include<cstring>#include<map>#include<stack>#include<set>#include<vector>#include<algorithm>#include<string.h>typedef long long ll;typedef unsigned long long LL;using namespace std;const int INF=0x3f3f3f3f;const double eps=0.0000000001;const int N=100000+10;const int MAX=1000+10;int vis[N];double x[N],y[N],z[N];double w[MAX][MAX],v[MAX][MAX];double low[N];int n;double fun(double a,double b,double c,double d){ double ans=(a-c)*(a-c)+(b-d)*(b-d); return sqrt(ans);}int prime(double x){ double sum=0; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++)low[i]=v[0][i]-x*w[0][i]; vis[0]=1; for(int i=0;i<n;i++){ int k; double maxx=INF*1.0; for(int j=0;j<n;j++)if(vis[j]==0&&low[j]<maxx){ maxx=low[j]; k=j; } if(maxx==1.0*INF)break; sum=sum+maxx; vis[k]=1; for(int j=0;j<n;j++) if(vis[j]==0&&low[j]>v[k][j]-x*w[k][j]) low[j]=v[k][j]-x*w[k][j]; } if(sum>0)return 1; else return 0;}int main(){ while(scanf("%d",&n)!=EOF){ if(n==0)break; double maxx=0; double minn=INF*1.0; for(int i=0;i<n;i++)scanf("%lf%lf%lf",&x[i],&y[i],&z[i]); for(int i=0;i<n;i++) for(int j=i+1;j<n;j++){ double t=fun(x[i],y[i],x[j],y[j]); w[i][j]=w[j][i]=t; v[i][j]=v[j][i]=fabs(z[i]-z[j]); maxx=max(v[i][j],maxx); minn=min(minn,t); } double low=0.0; double high=maxx/minn; double ans; while(low+eps<high){ double mid=(low+high)/2; if(prime(mid)){ ans=mid; low=mid; } else high=mid; } printf("%.3f\n",ans); }}
POJ 2728(最优比率生成树+01规划)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。