hdu 1059(多重背包)

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int F[210000];
int V;

void ZeroOnePack(int cost, int weight){
    for(int i = V; i >= cost; i --)
        if(F[i-cost] + weight > F[i])
            F[i] = F[i-cost] + weight;
}

void CompletePack(int cost, int weight){
    for(int i = cost; i <= V; i ++)
        if(F[i-cost] + weight > F[i])
            F[i] = F[i-cost] + weight;
}

void MultiplePack(int cost, int weight, int amount){
    if(cost*amount >= V) CompletePack(cost, weight);
    else {
        for(int i = 1; i < amount; ){
            ZeroOnePack(i*cost, i*weight);
            amount -= i;
            i <<= 1;
        }
        ZeroOnePack(amount*cost, amount*weight);
    }
}

int main()
{
    int a[10];
    int flag = 1;
    while(scanf("%d%d%d%d%d%d", &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) != EOF){
        if(a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0 && a[5] == 0 && a[6] == 0) break;
        cout << "Collection #" << flag ++ << ":" << endl;
        V = 0;
        for(int i = 1; i <= 6; i ++)
            V += i * a[i];
        if(V % 2 == 1) {
                cout << "Can't be divided." << endl << endl;
                continue;
        }
        else {
            V /= 2;
            memset(F, 0, sizeof(F));
            for(int i = 1; i <= 6; i ++)
                MultiplePack(i, i, a[i]);
            if(F[V] != V) cout << "Can't be divided." << endl << endl;
            else cout << "Can be divided." << endl << endl;

        }
    }
    return 0;
}

  参照背包问题九讲。。

posted @ 2013-10-18 17:47  i梦  阅读(179)  评论(0)    收藏  举报