BZOJ.1017.[JSOI2008] Warcraft map (tree DP backpack DP)

topic link

Tree-shaped DP, considering the contribution of child nodes to parent nodes.
Let f[x][i][j] represent the current x, and use i x to synthesize the equipment of the previous layer, and the cost is the maximum value of j.
When it is transferred from the child node, it is a grouped backpack, and an auxiliary array g[i][j] is required to represent the maximum value that the previous i subtrees cost j can contribute to x.
Then \(g[i][j] = max{g[i-1][jk]+f[v][l*need[x]][k]}\) . \(need[x]\) is the number of child nodes v required by x, and \(l\) is the number of composite x, which also requires enumeration.
Then for each \(l\) , you can enumerate how many x's are used to synthesize the previous layer, and update f, that is, \(f[x][i][j] = max{g[all][j]+val [x]*(li)}\) . ( \(g\) is already the value required to synthesize \(l\) x)
After processing a subtree, you can simply pack the maximum value. .

g[][] can indeed use one dimension, but it cannot be further optimized, and every memset cannot be avoided. (optimization is fairly obvious)

Expense flow obviously can't do it. .

After optimization: 43108kb 1100ms

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=53,M=2002,INF=0x3f3f3f3f;

int n,m,Enum,H[N],nxt[N],to[N],need[N],dgr[N],val[N],cost[N],num[N],Ans[M],f[N][101][M],g[N][M];

inline int read()
{
    int now=0;register char c=gc();
    for(;!isdigit(c);c=gc());
    for(;isdigit(c);now=now*10+c-'0',c=gc());
    return now;
}
inline void AddEdge(int u,int v,int w){
    ++dgr[v], to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum, need[Enum]=w;
}
void dfs(int x)
{
    if(!H[x]){
        num[x]=std::min(num[x],m/cost[x]);
        for(int i=0; i<=num[x]; ++i)
            for(int j=i; j<=num[x]; ++j)//限制不是 j*cost[x]<=m。。还有num[]限制。
                f[x][i][j*cost[x]]=(j-i)*val[x];
        return;
    }
    num[x]=100;
    for(int i=H[x]; i; i=nxt[i])
        dfs(to[i]), num[x]=std::min(num[x],num[to[i]]/need[i]), cost[x]+=need[i]*cost[to[i]];
    num[x]=std::min(num[x],m/cost[x]);
    memset(g,-0x3f,sizeof g);
    g[0][0]=0;
    for(int now,l=num[x]; ~l; --l)//当前要合成l个 
    {
        now=0;
        for(int i=H[x],v=to[i],w=l*need[i]; i; i=nxt[i],v=to[i],w=l*need[i],++now)
            for(int j=0; j<=m; ++j)
                if(g[now][j]>=0)//这个优化很有效 
                    for(int k=0; k+j<=m; ++k)//g[0][..]不会被更新了,每次一定是由之前合法的转移来 
                        g[now+1][j+k]=std::max(g[now+1][j+k],g[now][j]+f[v][w][k]);
        for(int i=0; i<=l; ++i)
            for(int j=0; j<=m; ++j)
                f[x][i][j]=std::max(f[x][i][j],g[now][j]+(l-i)*val[x]);//不需要再枚举k用g[k]更新f[j-k],f只是由g[]转移来,且只能从当前g[]转移。
    }
}

int main()
{
    n=read(),m=read();
    char s[3];
    for(int x,v,i=1; i<=n; ++i)
    {
        val[i]=read(), scanf("%s",s);
        if(s[0]=='A'){
            x=read();
            while(x--) v=read(),AddEdge(i,v,read());
        }
        else cost[i]=read(),num[i]=read();
    }
    memset(f,-0x3f,sizeof f);//会有非法状态。
    for(int i=1; i<=n; ++i)
        if(!dgr[i])
        {
            dfs(i);
            for(int j=m; j; --j)
                for(int k=1; k<=j; ++k)
                    Ans[j]=std::max(Ans[j],Ans[j-k]+f[i][0][k]);//根节点从f[i][0]转移就好了。
        }
    printf("%d",Ans[m]);
    return 0;
}

Before optimization: 42700kb 6968ms

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=53,M=2002,INF=0x3f3f3f3f;

int n,m,Enum,H[N],nxt[N],to[N],need[N],dgr[N],val[N],cost[N],num[N],Ans[M],f[N][101][M],g[M];

inline int read()
{
    int now=0;register char c=gc();
    for(;!isdigit(c);c=gc());
    for(;isdigit(c);now=now*10+c-'0',c=gc());
    return now;
}
inline void AddEdge(int u,int v,int w){
    ++dgr[v], to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum, need[Enum]=w;
}
void dfs(int x)
{
    if(!H[x]){
        num[x]=std::min(num[x],m/cost[x]);
        for(int i=0; i<=num[x]; ++i)
            for(int j=i; j<=num[x]; ++j)//限制不是 j*cost[x]<=m。。还有num[]限制。
                f[x][i][j*cost[x]]=(j-i)*val[x];
        return;
    }
    num[x]=100;
    for(int i=H[x]; i; i=nxt[i])
        dfs(to[i]), num[x]=std::min(num[x],num[to[i]]/need[i]), cost[x]+=need[i]*cost[to[i]];
    num[x]=std::min(num[x],m/cost[x]);
    for(int l=num[x]; ~l; --l)//当前要合成l个 
    {//g[]不需要再清空了吧,数量递减g[]一定是递增的。当然这么写本来也不需要。
        memset(g,-0x3f,sizeof g);//然而必须要清空。。之后的g[j]可能被之前本应非法-INF的g[j-k]给更新了。。
        g[0]=0;//necessary
        for(int i=H[x]; i; i=nxt[i])
            for(int j=m; ~j; --j)//这要更新到0!(DP g[]的初始值) 
            {
                int tmp=-INF;
                for(int k=0; k<=j; ++k)
                    tmp=std::max(tmp,g[j-k]+f[to[i]][l*need[i]][k]);
                g[j]=tmp;
            }
        for(int i=0; i<=l; ++i)
            for(int j=0; j<=m; ++j)
                f[x][i][j]=std::max(f[x][i][j],g[j]+(l-i)*val[x]);//不需要再枚举k用g[k]更新f[j-k],f只是由g[]转移来,且只能从当前g[]转移。
    }
}

int main()
{
    n=read(),m=read();
    char s[3];
    for(int x,v,i=1; i<=n; ++i)
    {
        val[i]=read(), scanf("%s",s);
        if(s[0]=='A'){
            x=read();
            while(x--) v=read(),AddEdge(i,v,read());
        }
        else cost[i]=read(),num[i]=read();
    }
    memset(f,-0x3f,sizeof f);//会有非法状态。
    for(int i=1; i<=n; ++i)
        if(!dgr[i])
        {
            dfs(i);
            for(int j=m; j; --j)
                for(int k=1; k<=j; ++k)
                    Ans[j]=std::max(Ans[j],Ans[j-k]+f[i][0][k]);//根节点从f[i][0]转移就好了。
        }
    printf("%d",Ans[m]);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324939932&siteId=291194637