首页 > 其他 > 详细

POJ 1014 Dividing

时间:2014-04-05 03:10:24      阅读:583      评论:0      收藏:0      [点我收藏+]

多重背包..看代码吧还是orz

bubuko.com,布布扣
#include <iostream>
using namespace std;

#define LEN 7
#define INIT 0xffffffff
int F[60001];

int max(int x, int y)
{
    return x>y?x:y;
}

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

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

void MultiPack(int cost, int weight, int V, int amount)
{
    if (cost*amount>=V) {
        CompletePack(cost, weight, V);
        return;
    }
    int k = 1;
    while (k<amount) {
        ZeroOnePack(cost*k, weight*k, V);
        amount -= k;
        k *=2;
    }
    ZeroOnePack(cost*amount, weight*amount, V);
}

int main(int argc, char **argv)
{
    int index = 0;
    int num[LEN];
    while (1) {
        ++index;
        int sum = 0;
        int bin = 0;
        for (int i=1; i<LEN; ++i) {
            cin>>num[i];
            sum += i * num[i];
            bin |= num[i];
        }
        if (!bin)
            break;
        if (sum&0x01)
            cout<<"Collection #"<<index<<":\n"<<"Can‘t be divided."<<endl<<endl;
        else {
            int V = sum>>1;
            F[0] = 0;
            for (int i=1; i<=V; ++i)
                F[i] = INIT;
            for (int i=1; i<LEN; ++i)
                MultiPack(i, i, V, num[i]);
            if(F[V]!=V)
                cout<<"Collection #"<<index<<":\n"<<"Can‘t be divided."<<endl<<endl;
            else
                cout<<"Collection #"<<index<<":\n"<<"Can be divided."<<endl<<endl;
        }
    }

    return 0;
}
View Code

 

POJ 1014 Dividing,布布扣,bubuko.com

POJ 1014 Dividing

原文:http://www.cnblogs.com/man1304010109/p/3644892.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!