Bzoj 1649
Luogu 2854
From:usaco 2006 Dec Sliver (question 10th of Usaco brush problem)
Obviously two-dimensional cost 01 backpack.
But direct doing will time out, and there are restrictions on the connection.
You can tell by the wi[i]+xi[i]=j that you only need to consider the transfer wi[i]+xi[i]=j
So we set F[I][J] f[i][j] for the maximum interesting value of J J for the shop to I I
Then there are the following transfer equations
F[xi[i]+wi[i]][j]=max (F[xi[i]+wi[i]][j],f[xi[i]][j−ci[i]]+fi[i]) f[xi[i]+wi[i]][j] = max (f[xi[i]+wi[i]][j), f[xi[i ]][j-ci[i]]+fi[i])
At the same time, the use of this will be aftereffect, we sort the array by Xi Xi, and then in this order F[xi[i] must be the updated state so that no effect
Note that the equation initialization is all −inf-inf, except f[0][0]=0 f[0][0]=0, because the state of transfer from f[0][0] f[0][0 is feasible, otherwise there may be a problem of orbital connectivity.
#include <cstdio> #include <cstring> #include <algorithm> #include <stack> #include <vector
> #define MS (I, J) memset (i, J, sizeof i) #define LL-long using namespace std;
const int MAXN = 10000 + 5, MAXL = 1000 + 5;
struct Data {int XI, WI, FI, CI;
BOOL operator < (const data &b) Const {return XI < B.XI;
}}DI[MAXN];
int L, n, B;
LL f[maxl][maxl];//set F[i][j] for the shop to I, the cost is J void Clear () {MS (F,-127);
F[0][0] = 0;
} void Init () {clear ();
for (int i=1;i<=n;i++) {scanf ("%d%d%d%d", &di[i].xi, &di[i].wi, &di[i].fi, &DI[I].CI);
Sort (di+1, di+1+n);
} void Solve () {LL ans = 0; for (int i=1;i<=n;i++) {for (int j=b;j>=di[i].ci;j--) {F[di[i].xi+di[i].wi][j] = max (f[di[i].x
I+DI[I].WI][J], f[di[i].xi][j-di[i].ci]+di[i].fi);
ans = max (ans, f[l][j]); } if (ans>0) printf ("%lld\n", ans);
else printf (" -1\n"); int main (){#ifndef Online_judge freopen ("1.in", "R", stdin); Freopen ("1.out", "w", stdout);
#endif while (scanf ("%d%d%d", &l, &n, &b) ==3) init (), solve ();
return 0; }