hdu 3535 背包综合题

 1 /*
 2 有n组背包,每组都有限制
 3 0、至少选一项
 4 1、最多选一项
 5 2、任意选
 6 */
 7 #include <iostream>
 8 #include <cstdio>
 9 #include <cstring>
10 using namespace std;
11
12 const int maxn=105;
13 const int INF=0xfffffff;
14 int dp[maxn][maxn];//第i组,剩余时间j的最大价值
15 int w[maxn],v[maxn];
16 inline int max(int a,int b){return a>b?a:b;}
17 int main()
18 {
19     int n,t,m,flag,i,j,k;
20     while(~scanf("%d%d",&n,&t))
21     {
22         memset(dp[0],0,sizeof(dp));
23         for(i=1;i<=n;i++)
24         {
25             scanf("%d%d",&m,&flag);
26             for(j=1;j<=m;j++) scanf("%d%d",w+j,v+j);
27             if(flag==0)
28             {
29                 for(j=0;j<=t;j++) dp[i][j]=-INF;
30                 for(j=1;j<=m;j++)
31                     for(k=t;k-w[j]>=0;k--)
32                         dp[i][k]=max(dp[i][k],max(dp[i-1][k-w[j]]+v[j],dp[i][k-w[j]]+v[j]));//这组不选,第一次选,多次选
33             }
34             else if(flag==1)
35             {
36                 for(j=0;j<=t;j++) dp[i][j]=dp[i-1][j];
37                 for(j=1;j<=m;j++)
38                     for(k=t;k-w[j]>=0;k--)
39                         dp[i][k]=max(dp[i][k],dp[i-1][k-w[j]]+v[j]);//这组不选,第一次选
40             }
41             else
42             {
43                 for(j=0;j<=t;j++) dp[i][j]=dp[i-1][j];
44                 for(j=1;j<=m;j++)
45                     for(k=t;k-w[j]>=0;k--)
46                         dp[i][k]=max(dp[i][k],max(dp[i-1][k-w[j]]+v[j],dp[i][k-w[j]]+v[j]));//这组不选,第一次选,多次选
47             }
48         }
49         int ans=max(dp[n][t],-1);
50         printf("%d\n",ans);
51     }
52     return 0;
53 }
时间: 2024-09-29 09:44:20

hdu 3535 背包综合题的相关文章

HDU 3535 背包综合

题意:给3种背包,一种是至少装一个,一种是最多装一个,一种任意. 首先要对一维状态的原始背包很熟悉才可以.此处的i代表滚动的背包类型. 1. 任意的话就是01背包  初始化:dp[i][j]=dp[i-1][j].           dp[i][j]=max{dp[i][j]  ,  dp[i][ j-w[i] ]+v[i]    } dp[i][j-w[i]] 存在. 2. 最多装一个,就是比较替换.初始化:dp[i][j]=dp[i-1][j].   dp[i][j]=max{  dp[i

HDU 2602 Bone Collector(01背包裸题)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 60469    Accepted Submission(s): 25209 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bo

Jam&#39;s balance HDU - 5616 (01背包基础题)

Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side are the same weight. Weights can be put on left side or right side arbitrarily. Please tell whether the balance can measure an object of weight M. In

hdu 2955 Robberies (01背包好题)

Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 31769    Accepted Submission(s): 11527 Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that

HDU 1171 Big Event in HDU(01背包)

题目地址:HDU 1171 还是水题..普通的01背包.注意数组要开大点啊.... 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include &

HDU 3535 AreYouBusy

分组背包 最多取一个的组,则对于该组来说,每一个状态只可能由前一组的状态过来,分不取和取两种 至少取一个,则没了不取的选择,即没有dp[i][j]=dp[i-1][j]的递推式 任意取,即01背包,取法包括由前一组不取或取一个,或由该组取一个 dp[i][j]表示前i组,消耗j时间所能取得的最大值 初值赋为-1,表示该状态不可达,对于dp[0],初值赋为0,即什么都没有的时候最大的快乐值为0 还有一个要注意的点c[k]可能为0,会影响到递推式的顺序 发现做这种混合背包的题非常有助于弄清楚各种背包

hdu 4381 背包

http://acm.hdu.edu.cn/showproblem.php?pid=4381 Problem Description There are n boxes in one line numbered 1 to n, at the beginning, all boxes are black. Two kinds of operations are provided to you: 1 ai xi :You can choose any xi black boxes in interv

1085 背包问题(0-1背包模板题)

1085 背包问题(0-1背包模板题)(51NOD基础题) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为整数),与之相对应的价值为P1,P2--Pn(Pi为整数).求背包能够容纳的最大价值. Input 第1行,2个整数,N和W中间用空格隔开.N为物品的数量,W为背包的容量.(1 <= N <= 100,1 <= W <= 10000) 第2 - N + 1行,每行

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro