About the complete knapsack problem

Source: Internet
Author: User

or using dynamic programming (planning)

Problem Description:

--------------------------------------------------------------------------------------------------------------- ----------

Knapsack problem

There are n types of items, each with a weight and a value. but the number of each item is unlimited , at the same time there is a backpack, the maximum load weight of k, today from n items selected several pieces (the same item can be selected multiple times), so that its weight and less than equal to K, and the value of the maximum.
Input data:
The first row two number: The total number of items n, the backpack load weight k; two numbers are separated by a space;
The second row n number, is n kind of item weight; two numbers are separated by a space;
The number of the third row n is the value of n items; Two numbers are separated by a space.
Output data:
The total value of the first line.
Input Sample:
4 10
2 3 4 7
1 3 5 9
Sample output:
12

--------------------------------------------------------------------------------------------------------------- ----------

Law One:

1#include"iostream"2#include"Cstdio"3 #defineMAXN 81020504 using namespacestd;5 intN,W[MAXN],V[MAXN];6 intK;7 intF[MAXN];8 intMain ()9 Ten {    OneCin>>n>>K; A      for(intI=1; i<=k;i++) -f[i]=-2100000; -      for(intI=1; i<=n;i++) theCin>>W[i]; -      for(intI=1; i<=n;i++) -Cin>>V[i]; -f[0]=0; +      for(intI=1; i<=n;i++) -      for(intwei=w[i];wei<=k;wei++) +F[wei]= (F[wei-w[i]]+v[i]>f[wei])? f[wei-w[i]]+V[i]:f[wei]; Acout<<F[k]; at     return 0; -}

Law II:

Cond....

About the complete knapsack 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.