Mixed backpack problem

Source: Internet
Author: User

Actually, it's even easier to mix backpacks.

There are n kinds of goods, the capacity of the backpack is V, next gives the weight of each item w[i], the value of v[i], the number of c[i], if C[i] is 0, indicating that this article has no number of pieces, to maximize the value

Obviously, the DP equation is divided into two types

If it's a full backpack, it's

Dp[j]=max (Dp[j],dp[j-w[i]]+v[i]) (j=w[i];j<=v;j++)

If it's a 01 or a multi-pack,

Dp[j]=max (Dp[j],dp[j-k*w[i]]+k*v[i]) (j-k*w[i]>=0 && 0<=k<=c[i])

Obvious

The code is as follows

#include <cstdio>#include<cstdlib>#include<algorithm>#include<iostream>#include<cstring>using namespacestd;intn,v,dp[10010],v[10005],w[10005],c[10005];intMain () {scanf ("%d%d",&v,&N);  for(intI=1; i<=n;i++) {scanf (" %d%d%d",&w[i],&v[i],&C[i]); }     for(intI=1; i<=n;i++)    {        if(c[i]==0)        {             for(intj=w[i];j<=v;j++) {Dp[j]=max (dp[j],dp[j-w[i]]+V[i]); }        }        Else        {             for(intj=v;j>=w[i];j--)            {                 for(intk=0; K<=c[i] && j-k*w[i]>=0; k++) {Dp[j]=max (dp[j],dp[j-k*w[i]]+k*V[i]); } }}} printf ("%d\n", Dp[v]);}

Mixed backpack problem

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.