Home > Back-end >  The multiple knapsack problem I
The multiple knapsack problem I

Time:12-25

Which bosses know where this code is wrong!
#include
#include
#include
using namespace std;
Int main ()
{
Int N, V.
Int, v, w, s;
Cin & gt;> N & gt;> V;
Int dp [110];
int i, j, k;
For (int j=1; J & lt;=N; J++) {
Dp [j]=0;
}
For (I=1; I & lt;=N; I++) {
Cin & gt;> V & gt;> W & gt;> s;
For (j=V; J & gt;=0; J -) {
For (k=1; K & lt;=s & amp; & K * v & lt;=j; K++)
{
Dp [j]=Max (dp) [j], dp [j] - k * v * w + k);
}
}
}
Cout & lt; return 0;
}
The correct code:
#include
#include
#include

using namespace std;
Const int A=110;
Int dp [A];
Int main ()
{
Int N, V.
Int, v, w, s;
Cin & gt;> N & gt;> V;

int i, j, k;
For (int j=1; J & lt;=N; J++) {
Dp [j]=0;
}
For (I=1; I & lt;=N; I++) {
Cin & gt;> V & gt;> W & gt;> s;
For (j=V; J & gt;=0; J -) {
For (k=1; K & lt;=s & amp; & K * v & lt;=j; K++)
{
Dp [j]=Max (dp) [j], dp [j] - k * v * w + k);
}
}
}
Cout & lt; return 0;
}
Should be globally defined dp array of problems, which members can explain this week
  • Related