BZOJ 1017 魔兽地图
树形DP
F[i][j]表示用i钱上传j个
先造,再考虑私吞
#include <iostream>
using namespace std;
const int MAXN=55;
const int MAXM=2333;
const int MAXK=111;
const int INF=1034567890;
int N, M;
int ANS[MAXM];
int G[MAXM][MAXK];
struct Vert{
int Son, Bro, Fa;
int Need, Val, Lim, Price;
char Type;
int F[MAXM][MAXK];
Vert(){
Son=-1;Bro=-1;Fa=-1;
Need=0;Val=0;Lim=MAXK-1;Price=0;
Type='C';
for(int i=0;i<MAXM;++i) for(int j=0;j<MAXK;++j) F[i][j]=-INF;
F[0][0]=0;
}
} V[MAXN];
void Pre_DFS(int at){
//cout << at << " ";
for(int to=V[at].Son;to>0;to=V[to].Bro){
//cout << to << " ";
Pre_DFS(to);
V[at].Price+=V[to].Need*V[to].Price;
V[at].Lim=min(V[at].Lim, V[to].Lim/V[to].Need);
}
V[at].Lim=min(V[at].Lim, M/V[at].Price);
//cout << endl;
}
void DFS(int at){
for(int to=V[at].Son;to>0;to=V[to].Bro){
DFS(to);
for(int i=0;i<=M;++i) for(int j=0;j<=V[at].Lim;++j) G[i][j]=V[at].F[i][j];
if(to==V[at].Son){
for(int j=0;j<=V[at].Lim;++j) G[0][j]=0;
}
for(int i=0;i<=M;++i) for(int j=0;j<=V[at].Lim;++j) V[at].F[i][j]=-INF;
V[at].F[0][0]=0;
for(int j=0;j<=V[at].Lim;++j){
for(int i=0;i<=M;++i){
if(G[i][j]<0) continue;
for(int ii=M-i;ii>=0;--ii){
if(V[to].F[ii][j*V[to].Need]<0) continue;
V[at].F[ii+i][j]=max(V[at].F[ii+i][j], G[i][j]+V[to].F[ii][j*V[to].Need]);
}
}
}
}
for(int i=V[at].Lim-1;i>=0;--i)
for(int j=M;j>=0;--j)
V[at].F[j][i]=max(V[at].F[j][i], V[at].F[j][i+1]+V[at].Val);
/*
cout << "DFS " << at << " " << V[at].Lim << " " << V[at].Price << endl;
for(int i=0;i<=M;++i)
for(int j=0;j<=V[at].Lim;++j)
if(V[at].F[i][j]>=0) cout << ": " << i << " " << j << " " << V[at].F[i][j] << endl;
*/
}
int main(){
ios_base::sync_with_stdio(false);
cin >> N >> M;
for(int i=1, c;i<=N;++i){
cin >> V[i].Val;
cin >> V[i].Type;
if(V[i].Type=='A'){
cin >> c;
cin >> V[i].Son;
cin >> V[V[i].Son].Need;
V[V[i].Son].Fa=i;
for(int j=1, a=V[i].Son;j<c;++j, a=V[a].Bro){
cin >> V[a].Bro;
cin >> V[V[a].Bro].Need;
V[V[a].Bro].Fa=i;
}
}
else{
cin >> V[i].Price >> V[i].Lim;
V[i].Lim=min(V[i].Lim, M/V[i].Price);
for(int j=0;j<=V[i].Lim;++j)
V[i].F[V[i].Price*j][j]=0;
}
}
for(int i=1;i<=N;++i)
if(V[i].Need==0)
Pre_DFS(i), DFS(i);
ANS[0]=0;
for(int i=1;i<=N;++i)
if(V[i].Need==0)
for(int j=M;j>=0;--j)
for(int k=j;k>=0;--k)
ANS[j]=max(ANS[j], ANS[j-k]+V[i].F[k][0]);
/*
for(int i=0;i<=M;++i) cout << ANS[i] << " ";
cout << endl;
*/
for(int i=1;i<=M;++i) ANS[0]=max(ANS[0], ANS[i]);
cout << ANS[0] << endl;
return 0;
}
/*
10 59
5 A 3 6 1 9 2 10 1
1 B 5 3
1 B 4 3
1 B 2 3
8 A 3 2 1 3 1 7 1
1 B 5 3
5 B 3 3
15 A 3 1 1 5 1 4 1
1 B 3 5
1 B 4 3
33
*/