题目链接

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 组任务,T 个体力,每组任务有 m 个,分类为 type,每个任务花费 cose[] 体力,得到 value[] 的开心值,求最大开心值,若不能完成输出-1

     分类为 0:这一组中的 m 个任务至少选择一个。

     分类为 1:这一组中的 m 个任务最多选择一个。

     分类为 2:这一组中的 m 个任务随便选择。

定义:dp[i][k]:完成第 i 组任务时,体力为 k 时获得的开心值。

1.对于分类 0,若当前判断到一个任务 case,则有两种情况:

(1)它是该组第一个被选择的任务,则它更新的状态只能是将上一层的状态转移更新到当前位置。

dp[i][k]=max(dp[i][k],dp[i-1][k-cost[j]]+value[j]);

(2)它不是第一个被选择的任务,则它可以由当前组的状态转移更新到当前位置。

dp[i][k]=max(dp[i][k],dp[i][k-cost[j]]+value[j]);

为了方便判断处理第一个任务,初始化当前层为 -inf

2.对于分类 1,因为只能选一个或者不选,则它只能由上一层状态转移更新。

dp[i][k]=max(dp[i][k],dp[i-1][k-cost[j]]+value[j]);

3.对于分类 2,就是普通的 01背包问题

肯定是要从上层的转移过来:dp[i][k]=max(dp[i][k],dp[i-1][k-cost[j]]+value[j]);

但是还需要考虑到一点就是说,这个组里面的任务可以选取任意多的个数:

dp[i][k]=max(dp[i][k],dp[i][k-cost[j]]+value[j]);

代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
const long long N=200;
using namespace std;
const int inf=0x3f3f3f3f;
int dp[N][N];
int n,T;
int cost[N],value[N];
int main()
{
while(~scanf("%d%d",&n,&T))
{
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
{
int m,type;
scanf("%d%d",&m,&type);
for(int j=1; j<=m; j++)
scanf("%d%d",&cost[j],&value[j]);
if(type==0)//最少选择一个工作
{
for(int j=0; j<=T; j++)
dp[i][j]=-inf;//方便处理第一个任务,初始化为-inf
for(int j=1; j<=m; j++)
for(int k=T; k>=cost[j]; k--)
{
dp[i][k]=max(dp[i][k],dp[i][k-cost[j]]+value[j]);//不是改组中第一个任务,从改组中的其他任务更新来
dp[i][k]=max(dp[i][k],dp[i-1][k-cost[j]]+value[j]);//如果是改组中第一个被选择的任务,在从上一组更新来
}
}
else if(type==1)//选择一个或不选
{
for(int j=0; j<=T; j++)
dp[i][j]=dp[i-1][j];//只能由上一层的状态转移来
for(int j=1; j<=m; j++)
for(int k=T; k>=cost[j]; k--)
{
dp[i][k]=max(dp[i][k],dp[i-1][k-cost[j]]+value[j]);
}
}
else//可以任意的选择
{
for(int j=0; j<=T; j++)
dp[i][j]=dp[i-1][j];//从上一层转移过来
for(int j=1; j<=m; j++)
for(int k=T; k>=cost[j]; k--)
{
dp[i][k]=max(dp[i][k],dp[i][k-cost[j]]+value[j]);//由因为每组内的任务可以多选
dp[i][k]=max(dp[i][k],dp[i-1][k-cost[j]]+value[j]);//直接转移上一层的
}
}
}
dp[n][T]=max(dp[n][T],-1);
printf("%d\n",dp[n][T]);
}
return 0;
}

HDU 3535 AreYouBusy (混合背包之分组背包)的更多相关文章

  1. HDU 3535 AreYouBusy(混合背包)

    HDU3535 AreYouBusy(混合背包) http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意: 给你n个工作集合,给你T的时间去做它们.给你m和 ...

  2. hdu 3535 AreYouBusy 分组背包

    AreYouBusy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  3. [HDU 3535] AreYouBusy (动态规划 混合背包 值得做很多遍)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n个任务集合,需要在T个时间单位内完成.每个任务集合有属性,属性为0的代表至少要完成1个 ...

  4. HDU 1712 ACboy needs your help (分组背包模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...

  5. 【HDU】I love sneakers!(分组背包)

    看了许多的题解,都有题目翻译,很不错,以后我也这样写.直接翻译样例: /*鞋子的数量N[1, 100]; 拥有的金钱M[1, 1w]; 品牌数目[1, 10]*/ /*以下四行是对于每双鞋的描述*/ ...

  6. 【HDU - 4341】Gold miner(分组背包)

    BUPT2017 wintertraining(15) #8B 题意 给出每个黄金的坐标.价值及耗时,同一方向的黄金只能依次取,求T时间内收获的最大值. 题解 同一方向,物品前缀和构成的组合,相当于是 ...

  7. 洛谷 P1757 通天之分组背包 【分组背包】

    题目链接:https://www.luogu.org/problemnew/show/P1757#sub 题目描述 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包 ...

  8. HDU 3535 AreYouBusy (混合背包)

    题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...

  9. HDU 3535 AreYouBusy 经典混合背包

    AreYouBusy Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...

随机推荐

  1. windows多线程(十) 生产者与消费者问题

    一.概述 生产者消费者问题是一个著名的线程同步问题,该问题描述如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个具有多个缓冲区的缓冲池 ...

  2. Office/Visio/Project 2010 RTM (x86) (x64)(中文简体/英文)

    1.cn_office_professional_plus_2010_x64_515528.exe ed2k://|file|cn_office_professional_plus_2010_x64_ ...

  3. sublimeText3的一些操作记录

    # 给绿色版的sublimeText3添加右键菜单,其中@=“Sublime Text 3” 是右键展示的文字, 后面的icon是图标将下面代码保存为.reg文件执行 Windows Registry ...

  4. C# 键盘记录器

    设计背景 Win系统带有API可以获取键入值,本小程序主要应用了一个网上广为流传的类,可以说一个测试DEMO.有俗称为键盘钩子 设计思路 使用Win API获取建入值 相关技术 Win API 功能 ...

  5. 在 Linux服务器中安装 Python 3.6

    一.服务器环境配置 在 CentOS 7 中安装 Python 之前,请确保系统中已经有了所有必要的开发依赖: # yum -y groupinstall development # yum -y i ...

  6. MT【127】点对个数两题之一【图论】

    在平面上有\(n\) 个点$S={x_1,x_2\cdots,x_n}, $ 其中任意两个点之间的距离至少为 \(1\), 证明在这 \(n\) 个点中距离为 \(1\)的点对数不超过 \(3n\). ...

  7. 一些$LCT$的瓜皮题目

    一些瓜皮 放几个比较优(she)秀(pi)的\(LCT\)题. 老惯例,每一题代码因为一些未知原因消失了(如果要的话私我好了,虽然会咕咕咕). 嘴巴\(AC\)真香! [SP16580] QTREE7 ...

  8. [bzoj1875][SDOI2009] HH去散步 [dp+矩阵快速幂]

    题面 传送门 正文 其实就是让你求有多少条长度为t的路径,但是有一个特殊条件:不能走过一条边以后又立刻反着走一次(如果两次经过同意条边中间隔了别的边是可以的) 如果没有这个特殊条件,我们很容易想到dp ...

  9. java多线程 -- ReadWriteLock 读写锁

    写一条线程,读多条线程能够提升效率. 写写/读写 需要“互斥”;读读 不需要互斥. ReadWriteLock 维护了一对相关的锁,一个用于只读操作,另一个用于写入操作.只要没有 writer,读取锁 ...

  10. fzyzojP3782 -组合数问题

    这个ai<=2000有点意思 启发我们用O(W^2)的算法 FFT不存在,对应关系过紧 考虑组合意义转化建模,再进行分离 (除以2不需要逆元不懂为啥,但是算个逆元总不费事) 由于终点可能在起点的 ...