Desert King
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 28407 | Accepted: 7863 |
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
4 0 0 0 0 1 1 1 1 2 1 0 3 0
Sample Output
1.000
Source
就是前面写过的01分数规划应用到这里。每条边有一个cost和lenght属性,求使得SUM{c}/SUM{l} 最小的一颗生成树,也就是最优比率生成树。
我们可以二分这个最优比率x,如果x>=SUM{c}/SUM{l} ,说明x可行。化简后得到 SUM{c-x*l} <=0 ,将边的权值根据式子转化后求MST就好了,这里由于是个稠密图
,使用prim算法复杂度更低。
///一直没写过prim,注意d数组保存的是i到生成树的距离!别和最短路搞混= =
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define inf 0x3f3f3f3f 4 int N; 5 double eps=1e-6; 6 double x[1100],y[1100],z[1100]; 7 double dis(int a,int b){ 8 return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b])); 9 } 10 bool vis[1100]; 11 double d[1100]; 12 double l[1100][1100],c[1100][1100]; 13 const double INF=99999999999; 14 bool prime(double x){ 15 memset(vis,0,sizeof(vis)); 16 vis[1]=1; 17 for(int i=1;i<=N;++i) d[i]=c[1][i]-x*l[1][i]; 18 double res=0,minn=INF; 19 int u; 20 for(int i=1;i<N;++i){minn=INF; 21 for(int j=1;j<=N;++j) 22 if(!vis[j] && d[j]<minn){ minn=d[j]; u=j;} 23 24 if(minn==INF) break; 25 vis[u]=1; 26 res+=minn; 27 for(int j=1;j<=N;++j) 28 if(!vis[j] && d[j]>c[u][j]-x*l[u][j]) 29 d[j]=c[u][j]-x*l[u][j]; 30 } 31 return res<=0; 32 } 33 34 int main() 35 { 36 while(cin>>N){int i,j; 37 if(!N) break; 38 for(i=1;i<=N;++i) l[i][i]=c[i][i]=0; 39 for(i=1;i<=N;++i){ 40 scanf("%lf%lf%lf",x+i,y+i,z+i); 41 for(j=1;j<i;++j){ 42 c[i][j]=c[j][i]=fabs(z[i]-z[j]); 43 l[i][j]=l[j][i]=dis(i,j); 44 } 45 } 46 double l=0,r=100; 47 while(fabs(l-r)>=eps){ 48 double mid=(l+r)/2; 49 if(prime(mid))r=mid; 50 else l=mid; 51 } 52 printf("%.3f ",l); 53 } 54 return 0; 55 }