hdu3535 分组背包的研究

AreYouBusy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1833    Accepted Submission(s): 701


Problem Description
Happy New Term!
As having become a junior, xiaoA recognizes that there is not much time for her to AC problems, because there are some other things for her to do, which makes her nearly mad.
What's more, her boss tells her that for some sets of duties, she must choose at least one job to do, but for some sets of things, she can only choose at most one to do, which is meaningless to the boss. And for others, she can do of her will. We just define the things that she can choose as "jobs". A job takes time , and gives xiaoA some points of happiness (which means that she is always willing to do the jobs).So can you choose the best sets of them to give her the maximum points of happiness and also to be a good junior(which means that she should follow the boss's advice)?
 

Input
There are several test cases, each test case begins with two integers n and T (0<=n,T<=100) , n sets of jobs for you to choose and T minutes for her to do them. Follows are n sets of description, each of which starts with two integers m and s (0<m<=100), there are m jobs in this set , and the set type is s, (0 stands for the sets that should choose at least 1 job to do, 1 for the sets that should choose at most 1 , and 2 for the one you can choose freely).then m pairs of integers ci,gi follows (0<=ci,gi<=100), means the ith job cost ci minutes to finish and gi points of happiness can be gained by finishing it. One job can be done only once.
 

Output
One line for each test case contains the maximum points of happiness we can choose from all jobs .if she can’t finish what her boss want, just output -1 .
 

Sample Input
   
   
   
   
3 3 2 1 2 5 3 8 2 0 1 0 2 1 3 2 4 3 2 1 1 1 3 4 2 1 2 5 3 8 2 0 1 1 2 8 3 2 4 4 2 1 1 1 1 1 1 0 2 1 5 3 2 0 1 0 2 1 2 0 2 2 1 1 2 0 3 2 2 1 2 1 1 5 2 8 3 2 3 8 4 9 5 10
 

Sample Output
   
   
   
   
5 13 -1 -1
 
分组背包问题,
有n组物品,有的每组至少取一个,有的至多取一个,有的随意取。
#include <iostream>
#include <cstring>

using namespace std;

int n,T;
int m,s;
int c[111],g[111];
int f[222][222];
int num;

const int OO=99999999;

void intput();
void pack();

void input()
{
    cin>>m>>s;
    for (int i=1;i<=m;i++)
    {
        cin>>c[i]>>g[i];
    }
}

void pack()
{
    num++;
    if (s==0)
    {
        for (int i=0;i<=T;i++)
        {
            f[num][i]=-OO;
        }
        for (int i=1;i<=m;i++)
        {
            for (int j=T;j>=c[i];j--)
            {
                f[num][j]=max( f[num][j] , f[num][j-c[i]]+g[i] );
                f[num][j]=max( f[num][j] , f[num-1][j-c[i]]+g[i] );
            }
        }
    }
    else if (s==1)
    {
        for (int i=0;i<=T;i++)
        {
            f[num][i]=f[num-1][i];
        }
        for (int i=1;i<=m;i++)
        {
            for (int j=T;j>=c[i];j--)
            {
                f[num][j]=max( f[num][j] , f[num-1][j-c[i]]+g[i] );
            }
        }
    }
    else
    {
        for (int i=0;i<=T;i++)
        {
            f[num][i]=f[num-1][i];
        }
        for (int i=1;i<=m;i++)
        {
            for (int j=T;j>=c[i];j--)
            {
                f[num][j]=max( f[num][j] , f[num][j-c[i]]+g[i] );
            }
        }
    }
}

int main()
{
    while (cin>>n>>T)
    {
        num=0;
        memset(f,0,sizeof(f));
        memset(c,0,sizeof(c));
        memset(g,0,sizeof(g));
        for (int lp=1;lp<=n;lp++)
        {
            input();
            pack();
        }
        if ( f[num][T]>=0 ) cout<<f[num][T]<<endl;
        else cout<<"-1"<<endl;
    }
    return 0;
}

f[num][j]表示第num组背包容量为j时的最大价值
① 至少取一种的情况, f[ num ][]的初始值设为无穷小,
f[num][j]=max( f[num][j] , f[num][j-c[i]]+g[i] ,  f[num-1][j-c[i]]+g[i]  ); 
表示如果第num组容量j-c[i]尚未取过物品(f[num][j-c[i]]值为负数),则取第i件物品作为num第一件物品 f[num][j]=f[num-1][j-c[i]]+g[i],若该组已经取过物品(f[num][j-c[i]]值非负)则可以按普通的01背包处理将其加入背包。
② 至多取一种的情况,f[ num ][]初值赋为f[ num-1 ][],状态转移方程f[num][j]=max( f[num][j] , f[num-1][j-c[i]]+g[i] ),表示要么不取,要么只取第i个,从上一组的状态下取i。
③ 随意取的情况,直接按01背包处理。f[num][j]=max( f[num][j] , f[num][j-c[i]]+g[i] )

总结: f[num][j-c[i]]+g[i]表示将第i件物品加入当前背包,f[num-1][j-c[i]]+g[i]表示将第i件物品作为该组的第一件物品加入背包。f[num][]初值为负无穷时,至少会将一件物品加入背包,f[num][]初值与f[num-1][]相同时,有一件不取的情况出现。

你可能感兴趣的:(hdu3535 分组背包的研究)