http://poj.org/problem?id=2976

01分数规划问题,可以舍掉k组

01分数规划用于解决的经典问题是最优比率生成树

解法见http://www.cnblogs.com/lotus3x/archive/2009/03/21/1418480.html

#include  
#include 
#include 

using namespace std;

double eps = 1e-8;

double b[1005], c[1005], d[1005];

int main() {
    int n, k;
    while(~scanf("%d%d", &n, &k)) {
        if(!n && !k) break;
        for(int i = 0; i < n; i++)
            scanf("%lf", &b[i]);
        for(int i = 0; i < n; i++)
            scanf("%lf", &c[i]);
        double L = 0.0;
        double R = 1.0;
        double mid;
        while(R - L > eps) {
            mid = (L + R) / 2;
            for(int i = 0; i < n; i++)
                d[i] = b[i] - mid * c[i];
            double z = 0.0;
            sort(d, d + n);
            for(int i = k; i < n; i++)
                z += d[i];
            if(z > eps) L = mid;
            else R = mid;
        }
        printf("%d\n", (int)(mid*100+0.5));
    }
    return 0; 
}
View Code