HDU 3535 "Backpack"

Source: Internet
Author: User

Test instructions

Gives the n set of data with one type for each set of data.

0 represents at least one, 1 represents at most one, and 2 represents any selection.

Give the backpack capacity.

If the backpack does not meet the most basic requirements output-1.

Ideas:

Backpack Problem disguised Inspection ~

When 0 is initialized to-inf, then it is guaranteed to select at least one.

When 1 or 2 initializes the value of the previous layer, then 1 and 2 are slightly different, 1 can only get the next layer from the previous layer, 2 may be updated with this layer.

#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;intc[ the],w[ the];intdp[ the][ the];intMain () {intn,t;  while(SCANF ("%d%d", &n,&t)! =EOF) {memset (DP,0,sizeof(DP));  for(intI=1; i<=n;i++)        {            intm,s; scanf ("%d%d",&m,&s);  for(intj=1; j<=m;j++) {scanf ("%d%d",&w[j],&C[j]); }            if(s==0)            {                 for(intj=0; j<=t;j++) {Dp[i][j]=-999999999; }                 for(intj=1; j<=m;j++)                {                     for(intk=t;k>=w[j];k--)                    {                        //Dp[i][k]=max (Dp[i][k],dp[i-1][k-w[j]]+c[j]); //Dp[i][k]=max (Dp[i][k],dp[i][k-w[j]]+c[j]);Dp[i][k]=max (Dp[i][k],max (dp[i-1][k-w[j]]+c[j],dp[i][k-w[j]]+c[j])); }                }            }            Else if(s==1)            {                 for(intj=0; j<=t;j++) {Dp[i][j]=dp[i-1][j]; }                 for(intj=1; j<=m;j++)                {                     for(intk=t;k>=w[j];k--) {Dp[i][k]=max (dp[i][k],dp[i-1][k-w[j]]+C[j]); }                }            }            Else            {                 for(intj=0; j<=t;j++) {Dp[i][j]=dp[i-1][j]; }                 for(intj=1; j<=m;j++)                {                     for(intk=t;k>=w[j];k--) {Dp[i][k]=max (dp[i][k],dp[i][k-w[j]]+C[j]); }                }            }        }        if(dp[n][t]<0) printf ("-1\n"); Elseprintf ("%d\n", dp[n][t]); }}

HDU 3535 "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.