Description
Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels extremely bored. However, clever Zuosige comes up with a new game.
Zuosige likes elephants very much. Today he finds many types of elephant dolls. Each type has dolls in different colors or different sizes. Zuosige assigned a preference value to each of these dolls, and is willing to buy some. However, Zuosige has limited
money, so he can only choose at most one doll for each type. Please help Zuosige calculate the maximum total preference value of the dolls he can buy without exceeding his money limit.
Input
The first line contains one integer T, indicating the number of test cases.
In one test case, there are several lines.
In the first line, there are two integers N and M (1<=N<=20, 1<=M<=1000), indicating the number of types and the money he has.
In the following N lines, each line begin with an integer ci (1<=ci<=50), indicating the number of different dolls in the i-th type. Following in the same line there are 2*ci integers, every two of them describe a doll, in order
of wi and vi (1<=wi, vi<=1000), indicating the price and the preference value of the doll.
Output
For each test case, output one integer in one line indicating the answer. It is guaranteed that Zuosige can always buy one doll from each type.
Sample Input
1 3 15 4 4 3 5 1 4 3 2 7 5 2 2 4 6 4 4 1 7 5 3 3 3 3 4 1 4 10
Sample Output
24
HINT
Source
题意:有n种类型东西,每种类型有c个不同的物品,给出每个物品的价值与容量,每种类型的物品只能选一个问在背包大小为m的情况下能得到最大的价值是多少
分组背包模板题
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm> using namespace std; #define ls 2*i #define rs 2*i+1 #define up(i,x,y) for(i=x;i<=y;i++) #define down(i,x,y) for(i=x;i>=y;i--) #define mem(a,x) memset(a,x,sizeof(a)) #define w(a) while(a) #define LL long long const double pi = acos(-1.0); #define N 1000005 #define mod 19999997 const int INF = 0x3f3f3f3f; #define exp 1e-8 struct node { int v,w; }a[25][55]; int len[25]; int cmp(node a,node b) { return a.v*b.w>b.v*b.w; } int dp[1005]; int main() { int t,n,m,i,j,k; cin>>t; w(t--) { scanf("%d%d",&n,&m); up(i,1,n) { scanf("%d",&len[i]); up(j,1,len[i]) { scanf("%d%d",&a[i][j].w,&a[i][j].v); } } mem(dp,0); for(k = 1; k<=n; k++) { for(i = m; i>=0; i--) { for(j = 1;j<=len[k];j++) { if(a[k][j].w<=i) dp[i] = max(dp[i],dp[i-a[k][j].w]+a[k][j].v); } } } printf("%d\n",dp[m]); } return 0; }