HDU 3033 I love sneakers! Group backpack, hdusneakers
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;}
Help design a QQ group. There is a group with I love you and Chinese characters. If you are not a mainstream user, add extra points if you want to be nice-looking.
"S O
The hacker is familiar with it.
/
Chen/
Bytes
The hacker is quiet and leaves.
--- "--- Dear ------'
Bytes
Sorry,
Dp
Bytes
Always success>
The hacker leaves. It's a stranger.
Always success>
I Love you ~
Who will help me design a QQ group name?
〇 ╭ ●╮/╲
/Users \/days/
Dr. Huang/Chang/
/Chen/
Chen/
Bytes
Xiao Dian/Yu
,-, ˊ ,/,
. You '/JIU/
Forbidden. zookeeper/
<---- Zookeeper é ---- <--------- [I] ● _ ●