Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3033
There are n kinds of shoes, m yuan in the hand, and N kinds of shoes belong to k brands, (1 <= n <=, 1 <= m <=, 1 <= k <= 10), each pair of shoes has its own price and value, buy at least one pair of shoes for each brand and ask how many value shoes can be bought at most.
Train of Thought: the variant of the backpack problem divides the items to be loaded into several categories, and there are limits on the selection of each item (generally choose at least one or more, because all items are grouped, the results of the previous I-1 class subcontracting will be processed when Class I is processed.
State transition equation: for (I = 1; I <= K; I ++)
For (j = 0; j <top [I]; j ++)
For (k = m; k> = pro [I] [J] [0]; k --)
DP [I] [k] = max (DP [I] [K], DP [I-1] [k-Pro [I] [J] [0] + pro [I] [J] [1], DP [I] [k-Pro [I] [J] [0] + pro [I] [J] [1]);
Among them, I represents Category I, j Represents Category I, and K represents the space of the backpack. This transfer ensures that at least one item can be selected for each transfer result.
Code:
#include <iostream>#include <cstdio>#include <cstring>#define maxm 15#define maxn 105#define maxb 100005using namespace std;int pro[maxm][maxn][2];int dp[maxm][maxb],top[maxm];void init(){ memset(dp,-1,sizeof(dp)); memset(pro,0,sizeof(pro)); memset(top,0,sizeof(top)); dp[0][0]=0;}int main(){ int N,M,K,b; while(~scanf("%d%d%d",&N,&M,&K)) { init(); for(int i=0; i<N; i++) { scanf("%d",&b); scanf("%d%d",&pro[b][top[b]][0],&pro[b][top[b]][1]); top[b]++; } bool flag = 0; for(int i=1; i<=K; i++) { if(!top[i]) { flag = 1; break; } for(int j=0; j<top[i]; j++) for(int k=M; k>=pro[i][j][0]; k--) { if(dp[i][k-pro[i][j][0]]!=-1) dp[i][k]=max(dp[i][k-pro[i][j][0]]+pro[i][j][1],dp[i][k]); if(dp[i-1][k-pro[i][j][0]]!=-1) dp[i][k]=max(dp[i-1][k-pro[i][j][0]]+pro[i][j][1],dp[i][k]); } } int ans = -1; for(int i=0; i<=M; i++) if(dp[K][i]>ans) ans=dp[K][i]; if(ans==-1||flag) puts("Impossible"); else printf("%d\n",ans); } return 0;}
HDU 3033 I love sneakers! Group backpack