描述:

  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. poj2328---"right on"进入下一个case的模板(while)

    #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; ,end=; w ...

  2. Sprite Kit编程指南中文版下载

    下载地址:http://download.csdn.net/detail/xin814/6032573 关于Sprite Kit 重要提示:  这是API或开发技术的一个初版文档.虽然本文档的技术准确 ...

  3. overfllow的解析

    参数是scroll时候,必会出现滚动条.参数是auto时候,子元素内容大于父元素时出现滚动条.参数是visible时候,溢出的内容出现在父元素之外.参数是hidden时候,溢出隐藏.

  4. 项目代码摘抄,dot的用法之1

    function searchTags() { var list = $('#tags-list-select option:selected').val(); console.log(list); ...

  5. windows 7 memcached报failed to install service or service already installed的解决方案

    今天心血来潮捣鼓一下memcache,由于系统是windows 7,我参考了 Windows下安装Memcache 使用memcached for Win32. 在运行memcached.exe -d ...

  6. PLSQl远程连接oracle数据库

    PLSQL远程连接Oracle 10G 1.在安装ORACLE服务器的机器上搜索下列文件, ORACLE 服务器上的文件 oci.dll     ocijdbc10.dll     ociw32.dl ...

  7. 采用proguard困惑android代码

    当前是有些工具比方apktool,dextojar等是能够对我们android安装包进行反编译.获得源代码的.为了降低被别人破解,导致源代码泄露,程序被别人盗代替码,等等.我们须要对代码进行混淆.an ...

  8. cron 定时任务

    cron 是linux下的定时任务: M H D m d cmd.  这是一种cron文件格式.   M: 分钟(0-59). H:小时(0-23). D:天(1-31). m: 月(1-12). d ...

  9. apache+mysql+php环境的手动搭建

    一.搭建Apache Http Server 官方下载地址:http://www.apachehaus.com/cgi-bin/download.plx 搭建环境:win10 64位 WIN10 64 ...

  10. IOS 特定于设备的开发:检查设备接近度和电池状态

    UIDevice类提供了一些API,使你能够跟踪设备的特征,包括电池的状态和接近度传感器.他们二者都以通知的形式提供更新,可以订阅他们,以便在有重要的更新时通知你的应用程序. 1>启动和禁用接近 ...