二维0-1背包问题

   int MaxValue(int n,int j,int *w,int k,int *b,int *v,int ***m)
    {
    int t = max(w[n],b[n]);
    
    for(int i = 1;i    {
        for( int j = 1;j        {
            m[n][i][j] = 0;
        }
    }

    for(int i = t;i    {
        for(int j = t;j        {
            m[n][i][j] = v[n];
        }
    }

    for(int i = n-1;i>1;i--)
    {
        t = max(w[i],b[i]);
        for(int j1 = 1;j1        {
            for(int k1 = 1;k1            {
                m[i][j1][k1] = m[i+1][j1][k1];
            }
        }

        for(int j1 = t;j1<=j;j1++)
        {
            for(int k1 = t;k1<=k;k1++)
            {
                m[i][j1][k1] = max(m[i+1][j1][k1],m[i+1][j1-w[i]][k1-b[i]]+v[i]);
            }
        }

    }
    
    m[1][j][k] = m[2][j][k];
    if(m[2][j-w[1]][k-b[1]]+v[1]>m[1][j][k])
    {
        m[1][j][k] = m[2][j-w[1]][k-b[1]]+v[1];
    }

    return m[1][j][k];
    }

转载于:https://www.cnblogs.com/ITXIAZAI/p/4115110.html

你可能感兴趣的:(二维0-1背包问题)