本文主要是介绍Desert King POJ - 2728(最优比率生成树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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,代表在沙漠中有N个村庄;后面有N组数据,每个数据三个数字X,Y,Z代表村庄在位置(X,Y),且村庄的海拔是Z;
国王想要给每一个村庄供水,没修建一条路的花费是两个村庄之间的海拔之差;
问你想要使每一个村庄的都连接起来,建成的路的平均每公里的花费最小;
思路:
其实这道题是让你求出来 ai / bi 所有和的最小值;
那么假设 ai / bi >=x,那么ai - x*bi >= 0;
这道题可以使用最优比率生成树算法,其中用二分法来枚举 合适的 x 的值;
令F(x)=ai - x*bi,那么这就可以看成一个一元一次的方程式:F(x)=A - x*B,即F(x)= - B *x + A;
还是看这个图,用二分来枚举f(x) ;
因为是要求最小生成树,所以在每一次的二分判断中要跑一遍prim算法来判断现在的最小生成树的值是否小于0;
如果小于0,那么就是二分区间大了,缩小区间;
如果大于0,那么就是二分区间小了,扩大区间;
代码如下:
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;const double inf=1e18;
const int maxn=1e3+10;int n;
double cost[maxn][maxn];
double dis[maxn][maxn];
double x[maxn],y[maxn],z[maxn];
int vis[maxn];double get_dis(int a,int b)///计算两点之间的距离;
{return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}int check(double x)
{memset(vis,0,sizeof vis);double sum=0,lowcost[maxn];vis[1]=1;for(int i=1; i<=n; i++)///计算最小的每一单位距离的花费;lowcost[i]=cost[1][i]-x*dis[1][i];///prim算法;for(int i=2; i<=n; i++)///最小生成树,n-1条边;{double temp=inf;int k=-1;///记录是否还有最小的边;for(int j=2; j<=n; j++){if(!vis[j]&&lowcost[j]<temp){k=j;temp=lowcost[j];}}if(k==-1)break;vis[k]=1;sum+=temp;for(int j=2; j<=n; j++)///松弛;{if(!vis[j]&&cost[k][j]-x*dis[k][j]<lowcost[j])lowcost[j]=cost[k][j]-x*dis[k][j];}}if(sum>=0)return 1;elsereturn 0;
}int main()
{while(~scanf("%d",&n)&&n){for(int i=1; i<=n; i++){scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);}for(int i=1; i<=n; i++)///存储图的数据;{for(int j=i+1; j<=n; j++){dis[i][j]=dis[j][i]=get_dis(i,j);cost[i][j]=cost[j][i]=fabs(z[i]-z[j]);}}///二分枚举;double l=0.0,r=100.0;while(r-l>=1e-5){double mid=(l+r)/2;if(check(mid))l=mid;///扩大区间;elser=mid;///缩小区间;}printf("%.3f\n",l);///注意输出格式;}return 0;
}
这篇关于Desert King POJ - 2728(最优比率生成树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!