魔兽地图

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=2000+3;
const int N=55;
const int inf=0x3f3f3f3f;
int n,m;
int    L[N],P[N],C[N];
int nd[N];
bool ba[N];
struct node{
    int nxt,to;
}e[2*N];
int hd[N],cnt;
void add(int x,int y){
    e[++cnt].nxt=hd[x];
    e[cnt].to=y;
    hd[x]=cnt;
}
void dp(int x){
    if(ba[x]){
        L[x]=min(L[x],m/C[x]);
        return;
    }
    for(int i=hd[x];i;i=e[i].nxt){
        int y=e[i].to;
        dp(y);
        C[x]+=C[y]*nd[y];
        L[x]=min(L[x],L[y]/nd[y]);
    }
    L[x]=min(L[x],m/C[x]);
    
}
int f[N][105][M];
int g[N][M];
int rt;
bool du[N];
void dfs(int x){
    if(ba[x]){
        for(int l=0;l<=L[x];l++){
            for(int j=0;j<=l;j++){
                f[x][j][l*C[x]]=P[x]*(l-j);
            }
        }
    }
    
    for(int i=hd[x];i;i=e[i].nxt){
        int y=e[i].to; 
        dfs(y);
    }
    
    for(int l=0;l<=L[x];l++){
        int now=0;
        for(int i=hd[x];i;i=e[i].nxt){
            int y=e[i].to;
            now++;
            memset(g[now],0,sizeof g[now]);
            for(int j=l*C[x];j<=m;j++){
                for(int k=l*nd[y]*C[y];k<=j;k++){
                    g[now][j]=max(g[now][j],g[now-1][j-k]+f[y][l*nd[y]][k]);
                }
            }
        }
        for(int h=0;h<=l;h++){
            for(int k=l*C[x];k<=m;k++){
                f[x][h][k]=max(f[x][h][k],g[now][k]+(l-h)*P[x]);
            }
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    char op;int x,lim,s;
    memset(L,inf,sizeof L);
    for(int i=1;i<=n;i++){
        scanf("%d ",&P[i]);
        op=getchar();
        if(op=='B'){
            ba[i]=1;//is a leaf
            du[i]=1;
            scanf("%d%d",&C[i],&L[i]);
        }
        else{
            scanf("%d",&s);
            int son;
            for(int j=1;j<=s;j++){
                scanf("%d",&son);
                scanf("%d",&nd[son]);
                du[son]=1;
                add(i,son);
            }
        }
    }
    
    for(int i=1;i<=n;i++) if(!du[i]) rt=i;
    
    
    dp(rt);
    dfs(rt);
    int ans=0;
    for(int j=0;j<=L[rt];j++){
        for(int k=0;k<=m;k++){
            ans=max(ans,f[rt][j][k]);
        }
    }
    printf("%d",ans);
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/Miracevin/p/9568471.html
今日推荐