博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu3535 AreYouBusy
阅读量:5330 次
发布时间:2019-06-14

本文共 3684 字,大约阅读时间需要 12 分钟。

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

这道题用的知识比較多,是把三个类型结合起来。让我受益非常多。

首先用二维状态dp[i][j]表示前i组花费j元获得最多的价值,有三个状态方程,一个是至少一个,一个是至多一个。还有是任意,那么分情况讨论即可了。

这里要说一下的是对二维背包,第二和第三种情况都要先把上一行的值继承到这一行来。然后再dp,dp过程中。是对这一行值背包,还是对上一行的值背包。这点要考虑清楚。具体的在代码里了。

 
#include
#include
int max(int a,int b){ return a>b?a:b;}struct node{ int w,v;}a[106][106];int dp[106][106],n1[106],m1[106];int main(){ int n,m,i,j,k; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++){ scanf("%d%d",&n1[i],&m1[i]); for(j=1;j<=n1[i];j++){ scanf("%d%d",&a[i][j].w,&a[i][j].v); } } memset(dp,-1,sizeof(dp)); for(i=0;i<=m;i++){ dp[0][i]=0; } for(i=1;i<=n;i++){ if(m1[i]==2){//随便放 for(j=m;j>=0;j--){//先把上一行的值都储存下来 dp[i][j]=dp[i-1][j]; } for(k=1;k<=n1[i];k++){ for(j=m;j>=a[i][k].w;j--){ //if(dp[i-1][j-a[i][k].w]!=-1) //dp[i][j]=max(dp[i][j],dp[i-1][j-a[i][k].w]+a[i][k].v);注意这里不能这么写。由于这里是对i这组内背包,在i组上能够叠加 if(dp[i][j-a[i][k].w]!=-1) dp[i][j]=max(dp[i][j],dp[i][j-a[i][k].w]+a[i][k].v); } } } else if(m1[i]==1){//最多放一个 for(j=m;j>=0;j--){//和上面一样,先等于上一行的值 dp[i][j]=dp[i-1][j]; } for(j=m;j>=0;j--){ for(k=1;k<=n1[i];k++){ if(j>=a[i][k].w && dp[i-1][j-a[i][k].w]!=-1){//这里注意是对i-1这组背包。以为假设 dp[i-1][j-a[i][k].w]==-1。那么转移过来的状态就已经错了,这个状态的值肯定错了,并且这一组仅仅能放一个,不能在组内叠加。

dp[i][j]=max(dp[i][j],dp[i-1][j-a[i][k].w]+a[i][k].v);//这一行不能写成dp[i][j-a[i][k].w]+a[i][k].v,由于假设出现多个w[k]==0的情况。那么dp[i][j]就会一直改变,那么一定会取多个而不是最多取一个 } } } } else if(m1[i]==0){//至少放一个,这一组不能继承上一行的,以为上一行不为-1的,这一行不一定不为-1。由于这组要放一个以上才成立。和本行有关 for(k=1;k<=n1[i];k++){ for(j=m;j>=a[i][k].w;j--){ if(dp[i][j-a[i][k].w]!=-1){//这里由两个方程转移过来 dp[i][j]=max(dp[i][j],dp[i][j-a[i][k].w]+a[i][k].v); } if(dp[i-1][j-a[i][k].w]!=-1){ dp[i][j]=max(dp[i][j],dp[i-1][j-a[i][k].w]+a[i][k].v); } } } } } printf("%d\n",dp[n][m]); } return 0; }

转载于:https://www.cnblogs.com/gcczhongduan/p/5095834.html

你可能感兴趣的文章
显示地图
查看>>
无线通信基础(一):无线网络演进
查看>>
如何在工作中快速成长?阿里资深架构师给工程师的10个简单技巧
查看>>
WebSocket 时时双向数据,前后端(聊天室)
查看>>
关于cocoa 运行时runtime
查看>>
关于python中带下划线的变量和函数 的意义
查看>>
asp.net 写入excel时,不能更新。数据库或对象为只读。
查看>>
linux清空日志文件内容 (转)
查看>>
jsp中对jstl一些标签的引用方式
查看>>
安卓第十三天笔记-服务(Service)
查看>>
css3学习笔记之背景
查看>>
Servlet接收JSP参数乱码问题解决办法
查看>>
【bzoj5016】[Snoi2017]一个简单的询问 莫队算法
查看>>
[dpdk] 熟悉SDK与初步使用 (二)(skeleton源码分析)
查看>>
Ajax : load()
查看>>
分布式版本控制系统
查看>>
MySQL-EXPLAIN执行计划Extra解释
查看>>
Zookeeper概述
查看>>
Zookeeper一致性级别
查看>>
分布式系统的一致性级别划分及Zookeeper一致性级别分析
查看>>