Skip to content

Commit adcd234

Browse files
committed
knapsack updated
1 parent 71377b2 commit adcd234

File tree

1 file changed

+101
-59
lines changed

1 file changed

+101
-59
lines changed
+101-59
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11

22
/**
33
4-
Problem : 0-1 Knapsack
4+
F(i,W) = max( include ith item , exclude ith item )
55
6-
In 0-1 Knapsack problem, we are given a set of items, each with a weight and a value and
7-
we need to determine the number of each item to include in a collection so that
8-
the total weight is less than or equal to a given limit and the total value is as large as possible.
6+
Here ,
7+
include ith item = F(i+1,W+w[i]) + v[i]
8+
exclude ith item = F(i+1,W)
9+
10+
Base case:
11+
12+
F(i,W>MAX_WEIGHT) = INT_MIN
13+
F(i>n,W<=MAX_WIGHT) = 0
14+
F(i>n,W>MAX_WIGHT) = INT_MIN
915
1016
**/
1117

12-
/**Which of the favors of your Lord will you deny ?**/
18+
/** Which of the favors of your Lord will you deny ? **/
1319

1420
#include<bits/stdc++.h>
1521
using namespace std;
@@ -20,116 +26,152 @@ using namespace std;
2026
#define MP make_pair
2127
#define F first
2228
#define S second
23-
#define INF INT_MAX
2429

2530
#define ALL(x) (x).begin(), (x).end()
2631
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
32+
#define READ freopen("alu.txt", "r", stdin)
33+
#define WRITE freopen("vorta.txt", "w", stdout)
2734

2835
#include <ext/pb_ds/assoc_container.hpp>
2936
#include <ext/pb_ds/tree_policy.hpp>
3037
using namespace __gnu_pbds;
3138

32-
template<class TIn>
33-
using indexed_set = tree<
34-
TIn, null_type, less<TIn>,
35-
rb_tree_tag, tree_order_statistics_node_update>;
39+
template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>;
40+
41+
/**
3642
37-
/*
3843
PBDS
3944
-------------------------------------------------
4045
1) insert(value)
4146
2) erase(value)
4247
3) order_of_key(value) // 0 based indexing
4348
4) *find_by_order(position) // 0 based indexing
4449
45-
*/
50+
**/
51+
52+
template<class T1, class T2>
53+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
54+
template <class T>
55+
ostream &operator <<(ostream &os, vector<T>&v);
56+
template <class T>
57+
ostream &operator <<(ostream &os, set<T>&v);
4658

4759
inline void optimizeIO()
4860
{
4961
ios_base::sync_with_stdio(false);
5062
cin.tie(NULL);
5163
}
5264

53-
const int nmax = 100+7;
65+
const int nmax = 1e2+7;
66+
const int nmax2 = 1e5+7;
5467
const LL LINF = 1e17;
5568

56-
string to_str(LL x)
69+
template <class T>
70+
string to_str(T x)
5771
{
5872
stringstream ss;
5973
ss<<x;
6074
return ss.str();
6175
}
6276

63-
//bool cmp(const PII &A,const PII &B)
64-
//{
65-
//
66-
//}
67-
68-
vector<int> vara = { -1, 20, 5, 10, 40, 15, 25 };
69-
vector<int> wara = { -1, 1, 2, 3, 8, 7, 4 };
70-
int max_cap = 10;
77+
int n,MAX_WEIGHT;
7178

72-
/** 1 based indexing **/
79+
LL w[nmax];
80+
LL v[nmax];
81+
LL dp[nmax][nmax2];
7382

74-
/**
75-
76-
dp[i][j] = max(include the item , exclude the item)
77-
78-
**/
79-
80-
int dp[nmax][nmax];
81-
82-
int solve(int i,int w)
83+
LL solve(int pos,int W) /** maximum value to get using maximum weight W **/
8384
{
84-
if(w<0) return INT_MIN; /** **/
85-
86-
if(i==0 || w==0) return 0;
85+
if(W>MAX_WEIGHT) return INT_MIN; /** if anytime W>MAX_HEIGHT , not possible . This is used just to avoid RTE **/
8786

88-
int &ret = dp[i][w];
87+
if(pos>n) /** Done with n items and now check condition **/
88+
{
89+
return W<=MAX_WEIGHT ? 0 : INT_MIN;
90+
}
8991

92+
LL &ret = dp[pos][W];
9093
if(ret!=-1) return ret;
9194

92-
int inc = vara[i] + solve(i-1,w-wara[i]);
93-
int exc = solve(i-1,w);
95+
LL inc = solve(pos+1,W+w[pos]) + v[pos];
96+
LL exc = solve(pos+1,W);
9497

9598
return ret = max(inc,exc);
9699
}
97100

98-
string solve_print(int i,int w)
101+
string solve_print(int pos,int W)
99102
{
100-
if(w<0) return ""; /** **/
103+
if(W>MAX_WEIGHT) return "";
104+
if(pos>n) return "";
101105

102-
if(i==0 || w==0) return "";
106+
LL inc = solve(pos+1,W+w[pos]) + v[pos];
107+
LL exc = solve(pos+1,W);
103108

104-
int inc = vara[i] + dp[i-1][w-wara[i]];
105-
int exc = dp[i-1][w];
106-
107-
if(inc>exc)
108-
return to_str(vara[i]) + " " + solve_print(i-1,w-wara[i]);
109-
else
110-
return solve_print(i-1,w);
109+
if(inc>exc) return to_str(v[pos]) + " " + solve_print(pos+1,W+w[pos]);
110+
else return solve_print(pos+1,W);
111111
}
112112

113-
114113
int main()
115114
{
116115
optimizeIO();
117116

118-
memset(dp,-1,sizeof dp);
117+
cin>>n>>MAX_WEIGHT;
119118

120-
int n = vara.size()-1;
119+
for(int i=1;i<=n;i++)
120+
cin>>w[i]>>v[i];
121121

122-
cout<<solve(n,max_cap)<<endl;
123-
cout<<solve_print(n,max_cap)<<endl;
122+
memset(dp,-1,sizeof dp);
124123

125-
// for(int i=1; i<=n; i++)
126-
// {
127-
// for(int j=1; j<=max_cap; j++)
128-
// cout<<dp[i][j]<<" ";
129-
// cout<<endl;
130-
// }
124+
cout<<solve(0,0)<<endl;
125+
cout<<solve_print(0,0)<<endl;
131126

132127
return 0;
133128
}
134129

130+
/**
131+
132+
**/
133+
134+
template<class T1, class T2>
135+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
136+
{
137+
os<<"{"<<p.first<<", "<<p.second<<"} ";
138+
return os;
139+
}
140+
template <class T>
141+
ostream &operator <<(ostream &os, vector<T>&v)
142+
{
143+
os<<"[ ";
144+
for(int i=0; i<v.size(); i++)
145+
{
146+
os<<v[i]<<" " ;
147+
}
148+
os<<" ]";
149+
return os;
150+
}
151+
152+
template <class T>
153+
ostream &operator <<(ostream &os, set<T>&v)
154+
{
155+
os<<"[ ";
156+
for(T i:v)
157+
{
158+
os<<i<<" ";
159+
}
160+
os<<" ]";
161+
return os;
162+
}
135163

164+
//string solve_print(int i,int w)
165+
//{
166+
// if(w<0) return ""; /** **/
167+
//
168+
// if(i==0 || w==0) return "";
169+
//
170+
// int inc = vara[i] + dp[i-1][w-wara[i]];
171+
// int exc = dp[i-1][w];
172+
//
173+
// if(inc>exc)
174+
// return to_str(vara[i]) + " " + solve_print(i-1,w-wara[i]);
175+
// else
176+
// return solve_print(i-1,w);
177+
//}

0 commit comments

Comments
 (0)