-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsackoptval.cpp
50 lines (42 loc) · 1.09 KB
/
knapsackoptval.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <cassert>
int main()
{
std::ifstream objectsfile("knapsack1.txt");
int W = 0;
objectsfile>>W; // knapsack weight/size limit
int n = 0;
objectsfile>>n; // total number of objects
std::cout<<W<<" "<<n<<" "<<std::endl;
std::vector< std::vector<int> > A(n+1, std::vector<int>(W+1,0));
for( int i=0; i<W+1; i++ )
{
A[0][i] = 0;
}
std::cout<<"2d A matrix initialized"<<std::endl;
for( int nObj=1; nObj<=n; nObj++ )
{
int curVal, curWt;
curVal = 0; curWt = 0;
objectsfile >> curVal >> curWt;
for( int nWt=0; nWt<=W; nWt++ )
{
if( nWt-curWt>=0 )
{
A[nObj][nWt] = std::max( A[nObj-1][nWt], A[nObj-1][nWt-curWt]+curVal );
}
else
{
A[nObj][nWt] = A[nObj-1][nWt];
}
}
}
objectsfile.close();
std::cout<<"optimal knapsack value = "<<A[n][W]<<std::endl;
return 0;
}