HDU 3033 I love sneakers! Group backpack

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.