# BZOJ1017 [JSOI2008]魔兽地图DotR 【树形dp + 背包dp】

BZOJ1017

## 题解

orz hzwer

$$f[i][j][k]$$表示$$i$$号物品恰好花费$$k$$金币，并将$$j$$个物品贡献给父亲的合成时的最大收益

#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;
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));
REP(i,n){
c = getchar(); while (c != 'A' && c != 'B') c = getchar();
if (c == 'A'){
typ[i] = 1;
while (x--){
build(i,to,w);
fa[to] = i;
}
}
}
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;
}


posted @ 2018-05-09 14:40  Mychael  阅读(133)  评论(0编辑  收藏  举报