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

考虑01分数规划,是不是很熟悉?
那么我们既可以用 二分来解决,或者用迭代来解决;

#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
#define maxn 200005
typedef long long ll;
#define inf 0x3f3f3f3f
#define eps 1e-6
#define ms(x) memset((x),0,sizeof(x))


inline int read()
{
    int x = 0, k = 1; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-')k = -1; c = getchar(); }
    while (c >= '0' && c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    return x * k;
}

ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}

int n;
int x[maxn],y[maxn],z[maxn];

int odis(int i,int j)
{
    return abs(z[i]-z[j]);
}

double ddis(int i,int j)
{
    return sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}

double prim(double r)
{
    bool vis[2000];
    double Min,disc,disd;
    double lowb[2000];
    int joint[2000];
    ms(vis);
    for(int i=1;i<=n;i++){
        joint[i]=1;
        lowb[i]=odis(1,i)-r*ddis(1,i);
    }

    lowb[1]=0;vis[1]=1;
    disc=0.0;disd=0.0;
    Min=1.0*inf;
    int k;
    for(int cnt=1;cnt<n;cnt++){
        Min=1.0*inf;k=1;
        for(int i=2;i<=n;i++){
            if(!vis[i]&&lowb[i]<Min){
                Min=lowb[i];k=i;
            }
        }
        vis[k]=1;lowb[k]=0;
        disc+=odis(k,joint[k]);disd+=ddis(k,joint[k]);

        for(int i=2;i<=n;i++){
            double w=odis(i,k)-r*ddis(i,k);
            if(!vis[i]&&w<lowb[i]){
               lowb[i]=w;joint[i]=k;
            }
        }

    }
    return disc/disd;

}


int main()
{
   //ios::sync_with_stdio(false);
   while(cin>>n&&n){
        for(int i=1;i<=n;i++){
            x[i]=read();y[i]=read();z[i]=read();
        }
        double ans1,ans2;
        ans1=0.0;
        while(1){
            ans2=prim(ans1);
            if(fabs(ans2-ans1)<=eps)break;
            ans1=ans2;
        }
        printf("%.3f\n",ans2);
   }
}















猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/81987633
今日推荐