BZOJ1017 [JSOI2008] Warcraft map DotR [tree dp + backpack dp]

topic link

BZOJ1017

answer

orz hzwer

tree dp gods

Let \(f[i][j][k]\) mean that the item \(i\) just spends \(k\) gold coins, and contributes the item \(j\) to the father's maximum profit when synthesizing

When calculating \(f[i][j][k]\) , we first enumerate and synthesize x number of \(i\) items, calculate the maximum income under the cost of various gold coins at this time,
and then enumerate \(j \le x\) and \(k\) , update \(f[i][j][k]\) to
calculate the maximum return, put the \(l\) th subtree \(f[ s][w * x][v]\) as the \ (v\ )th item of the \(l\) th item [ \(w * x\) is the contribution that the subtree needs to provide] do Group backpacks to calculate the maximum benefit you can get when you spend various coins

Since the overall relationship is uncertain, we can take out each tree root and make a grouped backpack again.

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = H[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define cls(s) memset(s,0,sizeof(s))
using namespace std;
const int maxn = 55,maxm = 2005,INF = 1000000000;
inline int read(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    return out * flag;
}
int H[maxn],ne = 1;
struct EDGE{int to,nxt,w;}ed[maxm];
inline void build(int u,int v,int w){
    ed[ne] = (EDGE){v,H[u],w}; H[u] = ne++;
}
int n,m,rt,f[maxn][101][maxm],g[maxn][maxm],h[maxn][maxm];
int W[maxn],P[maxn],M[maxn],typ[maxn],fa[maxn];
void dfs(int u){
    if (!typ[u]){
        M[u] = min(M[u],m / P[u]);
        for (int i = 0; i <= M[u]; i++)
            for (int j = 0; j <= i; j++)
                f[u][j][i * P[u]] = (i - j) * W[u];
        return;
    }
    M[u] = INF;
    Redge(u){
        dfs(to = ed[k].to);
        M[u] = min(M[u],M[to] / ed[k].w);
        P[u] += P[to] * ed[k].w;
    }
    M[u] = min(M[u],m / P[u]);
    memset(g,-0x3f3f3f3f,sizeof(g));
    g[0][0] = 0;
    for (int x = M[u]; x >= 0; x--){
        int tot = 0;
        Redge(u){
            ++tot; to = ed[k].to;
            for (int j = 0; j <= m; j++)
                for (int v = 0; v <= j; v++)
                    g[tot][j] = max(g[tot][j],g[tot - 1][j - v] + f[to][ed[k].w * x][v]);
        }
        for (int j = 0; j <= x; j++)
            for (int v = 0; v <= m; v++){
                if (v) g[tot][v] = max(g[tot][v],g[tot][v - 1]);
                f[u][j][v] = max(f[u][j][v],g[tot][v] + W[u] * (x - j));
            }
    }
}
int main(){
    memset(f,-0x3f3f3f3f,sizeof(f));
    n = read(); m = read(); char c;
    REP(i,n){
        W[i] = read();
        c = getchar(); while (c != 'A' && c != 'B') c = getchar();
        if (c == 'A'){
            typ[i] = 1;
            int x = read(),to,w;
            while (x--){
                to = read(); w = read();
                build(i,to,w);
                fa[to] = i;
            }
        }
        else P[i] = read(),M[i] = read();
    }
    int tot = 0;
    for (int u = 1; u <= n; u++)
        if (!fa[u]){
            dfs(u); ++tot;
            for (int i = 0; i <= m; i++)
                for (int j = 0; j <= i; j++)
                    h[tot][i] = max(h[tot][i],h[tot - 1][i - j] + f[u][0][j]);
        }
    int ans = 0;
    for (int i = 0; i <= m; i++) ans = max(ans,h[tot][i]);
    printf("%d\n",ans);
    return 0;
}

Guess you like

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