Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************
- * 0/1 Knapsack Code(DP). *
- ********************************************/
- int weight[MAX];
- int costs[MAX], profit[MAX][MAX];
- int knapsack(int items, int CAP)
- {
- for(int i = 1; i <= items; i++)
- {
- for(int w = 1; w <= CAP; w++)
- {
- if(weight[i] > w)
- profit[i][w] = profit[i-1][w];
- else
- profit[i][w] = max(profit[i-1][w],
- profit[i-1][w-weight[i]] + costs[i]);
- }
- }
- return profit[items][CAP];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement