描述:

  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)?

  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.

  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 .

代码:

  分组背包问题。组内有多种情况:至少选一个,至多选一个,无限制。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define N 110
#define MIN -1000000
int MAX(int a,int b,int c){
if( a<b ) a=b;
if( a<c ) a=c;
return a;
} int main(){
int n,t,val[N],time[N],dp[N][N];//dp第一维代表第n组物品;第二维代表背包容量,即时间
int m,s;
while( scanf("%d%d",&n,&t)!=EOF )
{
memset(dp,,sizeof(dp));//没有要求装满
for( int i=;i<=n;i++ )
{
scanf("%d%d",&m,&s);
for( int j=;j<=m;j++ )
scanf("%d%d",&time[j],&val[j]);
if( s== )//至少要选一个物品
{
for( int j=;j<=t;j++ )
dp[i][j]=MIN;//如果这一组的物品没有选,那么最后的解也将为负无穷,代表无解。
for( int j=;j<=m;j++ )//m个物品
{
for( int k=t;k>=time[j];k-- )//背包容量递减,k-time[j]也递减,这样保证数据不会重叠
{
//dp[i][k-time[j]]+val[j]:从这一组物品的上一个状态累加。组内决策
//dp[i-1][k-time[j]]+val[j]:直接从上一组物品累加,代表第一次加入该物品。从上一个分组开始决策
dp[i][k]=MAX(dp[i][k],dp[i][k-time[j]]+val[j],dp[i-][k-time[j]]+val[j]);
}
}
}
else if( s== )//最多选择一个
{
for( int j=;j<=t;j++ )
dp[i][j]=dp[i-][j];//可以不选择任何一个物品,最后的解即为上一组的解。
for( int j=;j<=m;j++ )
{
for( int k=t;k>=time[j];k-- )
dp[i][k]=max(dp[i][k],dp[i-][k-time[j]]+val[j]);//只能从上一个分组开始决策,代表最多选择一个
}
}
else if( s== ){//无限制
for( int j=;j<=t;j++ )
dp[i][j]=dp[i-][j];//可以不选择任何一个物品,最后的解即为上一组的解。
for( int j=;j<=m;j++ )
{
for( int k=t;k>=time[j];k-- )
{
//组内和组间决策
dp[i][k]=MAX(dp[i][k],dp[i][k-time[j]]+val[j],dp[i-][k-time[j]]+val[j]);
}
}
}
}
printf("%d\n",max(-,dp[n][t]));//无解
}
system("pause");
return ;
}

HDU3535-AreYouBusy的更多相关文章

  1. HDU3535——AreYouBusy

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题目意思:给出两个数n,T,分别表示有n个任务集合,T的总时间,对于每个任务集合有两个属性m和t ...

  2. HDU3535 AreYouBusy 混合背包

    题目大意 给出几组物品的体积和价值,每组分为三种:0.组内物品至少选一个:1.组内物品最多选一个:2.组内物品任意选.给出背包容量,求所能得到的最大价值. 注意 仔细审题,把样例好好看完了再答题,否则 ...

  3. HDU 3535 AreYouBusy(混合背包)

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

  4. hdu3535(AreYouBusy)

    题目链接:传送门 题目大意:有 n 组任务,m 个体力,每组任务有 k 个,分类为 f,每个任务花费 x 体力,得到 y 开心值,求最大开心值,若不能完成输出-1 分类为 0:这一组中的 k 个任务至 ...

  5. AreYouBusy

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

  6. hdu 3535 AreYouBusy 分组背包

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

  7. HDU 3535 AreYouBusy 经典混合背包

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

  8. AreYouBusy HDU - 3535 (dp)

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

  9. hdu3535题解

    hdu3535:http://acm.hdu.edu.cn/showproblem.php?pid=3535 该题是非常全面的一道分组背包问题.其实理解了最多一个的分组背包问题,解题起来也是很简单的. ...

  10. HDU 3535 AreYouBusy (混合背包)

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

随机推荐

  1. 如何让多个不同版本的jquery库共存

    问题描述:公司的登录接口使用的是jquery1.4.2,因为我要使用一个jquery.pagination的分页控件(jquery1.7.2).如果我使用了1.7.2,登录接口会有问题. <sc ...

  2. SQL高级查询

    高级查询: 一.多表链接 1,普通查询 select * from 表名,表名 where 表名.列名 = 表名.列名 2,join链接 select * from 表名 join 表名 on 表名. ...

  3. JavaSE思维导图(七)

  4. iOS中点击背景收键盘

    这一次给大家带来的是ios中点击背景如何收键盘(感觉不错给个赞

  5. Java日期计算之Joda-Time

    http://rensanning.iteye.com/blog/1546652 Joda-Time提供了一组Java类包用于处理包括ISO8601标准在内的date和time.可以利用它把JDK D ...

  6. BZOJ 1297: [SCOI2009]迷路( dp + 矩阵快速幂 )

    递推式很明显...但是要做矩阵乘法就得拆点..我一开始很脑残地对于每一条权值v>1的边都新建v-1个节点去转移...然后就TLE了...把每个点拆成9个就可以了...时间复杂度O((9N)^3* ...

  7. Hadoop插件安装

    1.首先下载Hadoop对应版本的插件,以Hadoop 1.0版本对应的插件Hadoop-eclipse-plugin1.0.3.jar为例 2.将下载的插件放置到Ecplise安装目录的plugin ...

  8. codinglife主题小修改和有意思的博客挂件

    这个主题很漂亮,不过为了迎合自己的喜好ヽ(•̀ω•́ )ゝ,修改了字号.阴影之类的小细节.同时下面还有我博客里面的两个有意思的小挂件,请向右边看(๑و•̀ω•́)و 1.主题修改:复制下面的css代码 ...

  9. PHP -- 添加注释

    PHP支持3种风格的注释 1.C++风格(//)的注释 这种注释不能出现?>标记,如果开启short_open和asp_tag设置,>和%>同样不能出现在注释中 <?php e ...

  10. latex 常用小结

    在写论文,甚至有些课程的报告的时候,latex是常用的工具.这篇博文简单的记录了latex常用的一些内容. 1 基本模块 没用过latex的读者,最想问的问题莫过于latex的 “hello worl ...