knapsack
C++
/* A Naive recursive implementation of 0-1 Knapsack problem */
class Knapsack
{
// A utility function that returns maximum of two integers
static int max(int a, int b) { return (a > b)? a : b; }
// Returns the maximum value that can be put in a knapsack of capacity W
static int knapSack(int W, int wt[], int val[], int n)
{
// Base Case
if (n == 0 || W == 0)
return 0;
// If weight of the nth item is more than Knapsack capacity W, then
// this item cannot be included in the optimal solution
if (wt[n-1] > W)
return knapSack(W, wt, val, n-1);
// Return the maximum of two cases:
// (1) nth item included
// (2) not included
else return max( val[n-1] + knapSack(W-wt[n-1], wt, val, n-1),
knapSack(W, wt, val, n-1)
);
}
// Driver program to test above function
public static void main(String args[])
{
int val[] = new int[]{60, 100, 120};
int wt[] = new int[]{10, 20, 30};
int W = 50;
int n = val.length;
System.out.println(knapSack(W, wt, val, n));
}
}
/*This code is contributed by Rajat Mishra */
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> >a;
//dp table is full of zeros
int n,s,dp[1002][1002];
void ini(){
for(int i=0;i<1002;i++)
for(int j=0;j<1002;j++)
dp[i][j]=-1;
}
int f(int x,int b){
//base solution
if(x>=n or b<=0)return 0;
//if we calculate this before, we just return the answer (value diferente of 0)
if(dp[x][b]!=-1)return dp[x][b];
//calculate de answer for x (position) and b(empty space in knapsack)
//we get max between take it or not and element, this gonna calculate all the
//posible combinations, with dp we won't calculate what is already calculated.
return dp[x][b]=max(f(x+1,b),b-a[x].second>=0?f(x+1,b-a[x].second)+a[x].first:INT_MIN);
}
int main(){
//fast scan and print
ios_base::sync_with_stdio(0);cin.tie(0);
//we obtain quantity of elements and size of knapsack
cin>>n>>s;
a.resize(n);
//we get value of elements
for(int i=0;i<n;i++)
cin>>a[i].first;
//we get size of elements
for(int i=0;i<n;i++)
cin>>a[i].second;
//initialize dp table
ini();
//print answer
cout<<f(0,s);
return 0;
}
Also in C++:
- Title
- c++ program how to let the user choose different game modes
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- c++ code for polynomial addition
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- how to print a string to console in c++
- Category
- C++
- Title
- clear file before writing c++
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- length of array in cpp
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- rgb(100,100,100,0.5) validation c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- set mimetype validation in mongoose
- Category
- C++
- Title
- c++ program to find gcd of 3 numbers
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- how read a shader from another file c++
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- Create a program that finds the minimum value in these numbers
- Category
- C++
- Title
- for c++
- Category
- C++
- Title
- trovare il valore massimo in un array c++ w3
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- for loop
- Category
- C++
- Title
- gcd in c++
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- passing a vector to a function c++
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- remove item from layout
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- clear qlayout
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- time conversion hackerrank solution in c++
- Category
- C++
- Title
- longest common subsequence
- Category
- C++
- Title
- convert char to string - c++
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- how to print for limited decimal values in c++
- Category
- C++
- Title
- c++ declare variable
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- howt o initialize 3d vector in c++
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- copy a part of a vector in another in c++
- Category
- C++
- Title
- C++ cin cout
- Category
- C++
- Title
- c++ get ascii value of char
- Category
- C++
- Title
- c++ replace n substrings
- Category
- C++
- Title
- print type cpp
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- int max in c++
- Category
- C++
- Title
- unordered_map c++
- Category
- C++
- Title
- how to import getline in c++
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- c++ yes no question
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- create copy constructor c++
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- c++ for loop
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- array<string, 7> c++
- Category
- C++
- Title
- iterative preorder traversal
- Category
- C++
- Title
- log base e synthax c++
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt
- Category
- C++
- Title
- ue4 c++ array
- Category
- C++
- Title
- flake8 max line length
- Category
- C++
- Title
- c++ give options string
- Category
- C++
- Title
- stringstream in c++
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- min heap priority queue c++
- Category
- C++
- Title
- map.erase in c++
- Category
- C++
- Title
- equal elements in two arrays in c++
- Category
- C++
- Title
- dijkstra in c++
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- roscpp publish int32
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- c++ how to make a negative float positive
- Category
- C++