HDU 3535 AreYouBusy(分组背包)

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

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


13 
-1 
-1

题目大意:

多组测试数据,输入a和b分别表示接下来有a种工作方式,有时间b做工作;输入n和m分别表示有n个工作,m表示工作方式,2随意选取工作,选其中多少都可以 
1最多选一个工作,0至少选一个工作;w数组存工作所需时间,p数组存工作获得开心值,求所得最大开心值。如果工作所需最短时间大于总时间输出-1。

C++

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,sum;
int w[110],p[110];
int dp[110][110];
int main()
{
    int a,b,c,d,e,f,k,l;
    while(cin>>a>>b)
    {
        memset(dp,0,sizeof(dp));
        for(e=1;e<=a;e++)
        {
            cin>>n>>m;
            for(c=1;c<=n;c++)
            {
                cin>>w[c]>>p[c];
            }
            if(m==0)
            {
                for(d=0;d<=b;d++)
                    dp[e][d]=-INF;
                for(k=1;k<=n;k++)
                {
                    for(d=b;d>=w[k];d--)
                        dp[e][d]=max(dp[e][d],max(dp[e][d-w[k]]+p[k],dp[e-1][d-w[k]]+p[k]));
                }
            }
            else if(m==1)
            {
                for(c=0;c<=b;c++)
                    dp[e][c]=dp[e-1][c];
                for(k=1;k<=n;k++)
                {
                    for(d=b;d>=w[k];d--)
                        dp[e][d]=max(dp[e][d],dp[e-1][d-w[k]]+p[k]);
                }
            }
            else if(m==2)
            {
                for(c=0;c<=b;c++)
                    dp[e][c]=dp[e-1][c];
                for(k=1;k<=n;k++)
                {
                    for(d=b;d>=w[k];d--)
                        dp[e][d]=max(dp[e][d],dp[e][d-w[k]]+p[k]);
                }
            }
        }
        dp[a][b]=max(dp[a][b],-1);
        cout<<dp[a][b]<<endl;
    }
    return 0;
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值