多重背包 单调队列优化

#include 
#include 
#include 
#define f(i,l,r) for(i=(l);i<=(r);i++)
using namespace std;
const int MAXN = 20005;
int n, m;
int f[MAXN], g[MAXN];
int q[MAXN];
int main()
{
    int i, j, k;
    cin >> n >> m;
    f(i, 1, n){
        int v, w, s;
        cin >> v >> w >> s;
        memcpy(g, f, sizeof f);
        f(k, 0, v - 1){
            int hd = 0, tl = -1;
            for(j = k; j <= m; j += v){
                if(hd <= tl && q[hd] < j - s * v) hd ++;
                while(hd <= tl && g[q[tl]] + (j - q[tl]) / v * w <= g[j]) tl --;
                q[++ tl] = j;
                if(hd <= tl) f[j] = g[q[hd]] + (j - q[hd]) / v * w;
            }
        }
    }
    cout << f[m] << endl;
    return 0;
}

 

你可能感兴趣的:(动态规划)