0% found this document useful (0 votes)
16 views

Daa Lab-Da4

The document discusses a variant of the knapsack problem where there are multiple objects of the same type that can be packed in the knapsack. It asks to design a dynamic programming solution to pack objects in the knapsack such that the total weight is <= the capacity and total profit is maximized. The code provided implements this solution using dynamic programming.

Uploaded by

Apoorva Saluja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Daa Lab-Da4

The document discusses a variant of the knapsack problem where there are multiple objects of the same type that can be packed in the knapsack. It asks to design a dynamic programming solution to pack objects in the knapsack such that the total weight is <= the capacity and total profit is maximized. The code provided implements this solution using dynamic programming.

Uploaded by

Apoorva Saluja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MIDTERM

DATA ALGORITHMS AND ANALYSIS


PROF: SUNIL KUMAR PV
NAME: APOORVA SALUJA
REG NO: 22BCE3169
QUESTION:

Knapsack Variant

Consider a knapsack of weight capacity W. We have n object


types. Assume that there is an infinite number of objects of each
type. You are allowed to pack multiple objects of the same type in
the knapsack. Within a given object type, all objects have the
same weight and the same profit. This would be wi and pi for the
object type i. Design a dynamic programming solution to this
variant of the knapsack problem. That is, you must pack objects
in the knapsack such that the total weight of the objects packed is
<=W and the total profit of the objects packed is as large as
possible.
Input Format
In the first line, read the total number of test cases T
In each test case read the following
Read the total number of object types n in the first line
Read the capacity of the knapsack W in the next line
Read the weights of each object type w1, w2, ..., wn in a space-
separated format in the next line
Read the profits of each object type p1, p2, ..., pn in a space-
separated format in the next line
Output Format
In the first line, print the maximum profit obtained
In the next line, print the count of each object type included in a
space-separated format
Sample Input
2
5
1000
21 23 24 22 24
23 25 24 24 22
8
2000
70 83 69 44 99 43 58 58
72 84 73 46 99 43 60 60
Sample Output
1094
40 6 0 1 0
2110
0 0 26 2 0 0 0 2

CODE:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int knapsack(int W,vector<int>&wt,vector <int>&val,int n)


{
vector <int> dp(W+1,0);
for(int i=0;i<=W;i++)
{
for(int j=0;j<n;j++)
{
if(wt[j]<=i)
dp[i]=max(dp[i],dp[i-wt[j]]+val[j]);
}
}
return dp[W];
}

int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int obj=0;
cin>>obj;
int W=0;
cin>>W;
vector <int>w(obj,0);
vector <int>p(obj,0);
for(int j=0;j<obj;j++)
{
cin>>w[j];
}
for(int j=0;j<obj;j++)
{
cin>>p[j];
}
int z=knapsack(W,w,p,obj);
cout<<z<<"\n";
}
return 0;
}

OUTPUT:

Displays the profits correctly.

You might also like