| Bzoj 1649| two-dimensional backpack | [Usaco2006 Dec] Cow Roller Coaster__bzoj

Source: Internet
Author: User

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, &AMP;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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.