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)?
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
5 13 -1 -1
这道题用的知识比較多,是把三个类型结合起来。让我受益非常多。
首先用二维状态dp[i][j]表示前i组花费j元获得最多的价值,有三个状态方程,一个是至少一个,一个是至多一个。还有是任意,那么分情况讨论即可了。
这里要说一下的是对二维背包,第二和第三种情况都要先把上一行的值继承到这一行来。然后再dp,dp过程中。是对这一行值背包,还是对上一行的值背包。这点要考虑清楚。具体的在代码里了。
#include<stdio.h> #include<string.h> 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 ",dp[n][m]); } return 0; }