I got the code in c++ for the backpack problem.
Can somebody explain the suboptimal solutions, and how the problem is solved.Code:#include <iostream> using namespace std; int main() { long long int B[1000],C[1000]; int V[100],Z[100],N,M,q,i; cin>>N>>M; for(i=1;i<=M;i++) cin>>Z[i]; for(i=1;i<=M;i++) cin>>V[i]; B[0]=0; C[0]=0; for (q=1;q<=N;q++) { B[q]=0; C[q]=0; for(i=1;i<=M;i++) if (Z[i]<=q) if (B[q-Z[i]]+V[i]>B[q]) { B[q]=B[q-Z[i]]+V[i]; C[q]=i; } } cout<<B[N]<<endl; return 0; }
Thanks in advance.
+karma for the person who will help me.




Reply With Quote
