题意:

  有n组工作,现在有T分钟时间去做一些工作。每组工作里有m个工作,并且类型为s,s类型可以为0,1,2,分别表示至少选择该组工作的一项,至多选择该工作的一项,不限制选择。每个工作有ci,gi两个属性,表示需要花费ci时间去完成该项工作,完成后将会获得gi的快乐值,现在求快乐值最大多少,如果不能完成工作,输出-1;

原题如下:

AreYouBusy
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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 (<=n,T<=) , 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 (<m<=), there are m jobs in this set , and the set type is s, ( stands for the sets that should choose at least job to do, for the sets that should choose at most , and for the one you can choose freely).then m pairs of integers ci,gi follows (<=ci,gi<=), 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 - . Sample Input Sample Output -
-

代码如下:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
#define INF (-99999)
const int Ni = ;
int dp[Ni][Ni];
int main()
{
int n,m,i,j,k,num,c,v,typ;
while(~scanf("%d%d",&n,&m))
{
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
{
scanf("%d%d",&num,&typ);
if(typ==) for(int p=;p<=m;p++) dp[i][p]=INF; else //把上一个包的值传递下去,目的是为了给某组可以选择不止一个的时候用
for(int p=;p<=m;p++) dp[i][p]=dp[i-][p];
for(j=;j<=num;j++)
{
scanf("%d%d",&c,&v);
for(k=m;k>=c;k--)
{
int a1 = dp[i][k-c]+v; //在已经选择该组的基础上,再次选择该组里的某个元素。
int a2 = dp[i-][k-c]+v; //在一个没有选择该组的基础上,选择该组里的元素
int a3 = dp[i-][k]; //不选择该组的元素。
if(typ==)//最少选一个
dp[i][k] = max(dp[i][k],max(a1,a2));
else if(typ==)//最多选一个
dp[i][k] = max(dp[i][k],max(a2,a3));
else //不限
dp[i][k] = max(dp[i][k],max(max(a1,a2),a3));
}
}
}
dp[n][m] = dp[n][m] > ? dp[n][m] : -;
printf("%d\n",dp[n][m]); }
return ;
}

背包系列 hdu 3535 分组背包的更多相关文章

  1. HDU 1712 分组背包

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. ACboy needs your help(HDU 1712 分组背包入门)

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. hdu 1712 (分组背包入门)

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 问题 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].这些物品被划分为若干组, ...

  4. HDU 3033 分组背包变形(每种至少一个)

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. HDU 4341 分组背包

    B - Gold miner Time Limit:2000MS      Memory Limit:32768KB     Description Homelesser likes playing ...

  6. HDU 3033 分组背包(至少选一个)

    分组背包(至少选一个) 我真的搞不懂为什么,所以现在就只能当作是模板来用吧 如果有大牛看见 希望评论告诉我 &代码: #include <cstdio> #include < ...

  7. 分组背包---P1757 通天之分组背包

    P1757 通天之分组背包 题解 分组背包板子题 k组物品,每组之间相互矛盾,也就是一组里面只能选一个或者不选 分组背包其实和01背包差不多,就是多加一维枚举组数 f[k][j] 前k组中,体积不超过 ...

  8. HDU 3535 分组混合背包

    http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n组工作,T时间,每个工作组中有m个工作,改组分类是s,s是0是组内至少要做一件,是1时最多做一件 ...

  9. HDU 3535 【背包】

    题意: 给出n组数据,每组数据有一个类型. 0代表至少选择一个,1代表至多选择一个,2代表任意选择. 给出背包容量. 如果背包不能满足最基本的要求输出-1. 思路: 背包问题变相考察~ 当0的时候初始 ...

随机推荐

  1. [bzoj1022/poj3480]小约翰的游戏John_博弈论

    小约翰的游戏John 题目大意:Nim游戏.区别在于取走最后一颗石子这输. 注释:$1\le cases \le 500$,$1\le n\le 50$. 想法:anti-SG游戏Colon定理. 如 ...

  2. eclipse 安装egit插件

    一.Eclipse上安装GIT插件EGit Eclipse的版本eclipse-java-helios-SR2-win32.zip(在Eclipse3.3版本找不到对应的 EGit插件,无法安装) E ...

  3. License使用成本估算

    License使用成本估算 Licmanager系统的成本估算模块是以參数估算法为基础的计算机成本估算软件,内部包括多个成本估算关系式,综合反映了license的使用特征.产品项目特征以组织经济环境等 ...

  4. 中文在C/C++中的处理和汉字乱码问题(wchar_t)

    中文字在C/C++中的处理 现在编程的语言和编程环境随着中国的发展開始对中文有进一步的支持.可是对中文的支持整体来说是有缺陷的,并且有与编译环境的不同导致中文在当前的C/C++中有非常多问题,并且非常 ...

  5. SQL 2012中文乱码

    MS SQL插入汉字的时候.常常会遇到部分汉字变成了乱码问号了, 所以在安装数据库或者在创建表的时候须要注意一下几点: 1.保存汉字的字段要用NVARCHAR.NCHAR.NTEXT等.插入的时候要用 ...

  6. swift 2.0 语法 可选类型

    import UIKit /*: 可选类型(可以有值, 也可以没有值) * 在OC中我们可以给一个对象类型变量赋值为nil或者一个对象, 而在Swift中如果想给一个变量赋值为nil那么必须明确指定为 ...

  7. libcurl库进行http通讯-一些主要的函数

    这里就简介一下libcurl的一些主要的函数. 调用curl_global_init()初始化libcurl 调用curl_easy_init()函数得到 easy interface型指针 调用cu ...

  8. [WebGL入门]二十一,从平行光源发出的光

    注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明.我会加上[lufy:],另外,鄙人webgl研究还不够深入,一些专业词语.假设翻译有误,欢迎大家指 ...

  9. HDU 1299Diophantus of Alexandria

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  10. codeforce1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) 题解

    秉承ACM团队合作的思想懒,这篇blog只有部分题解,剩余的请前往星感大神Star_Feel的blog食用(表示男神汉克斯更懒不屑于写我们分别代写了下...) C. Cloud Computing 扫 ...