BZOJ4182 Shopping(点分治+树形dp)

  点分治,每次考虑包含根的连通块,做树形多重背包即可,dfs序优化。注意题面给的di范围是假的,坑了我0.5h,心态炸了。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 510
#define M 4010
#define inf 1000000000
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int n,m,w[N],c[N],d[N],p[N],t,cnt,ans;
int size[N],dfn[N],id[N],f[N][M];
bool flag[N];
struct data{int to,nxt;
}edge[N<<1];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;} 
void getsize(int k,int from)
{
    size[k]=1;
    for (int i=p[k];i;i=edge[i].nxt)
    if (edge[i].to!=from&&!flag[edge[i].to]) 
    {
        getsize(edge[i].to,k);
        size[k]+=size[edge[i].to];
    }
}
int findroot(int k,int from,int s)
{
    int mx=0;
    for (int i=p[k];i;i=edge[i].nxt)
    if (edge[i].to!=from&&!flag[edge[i].to]&&size[edge[i].to]>size[mx]) mx=edge[i].to;
    if ((size[mx]<<1)>s) return findroot(mx,k,s);
    else return k;
}
void dfs(int k,int from)
{
    dfn[k]=++cnt;size[k]=1;id[cnt]=k;
    for (int i=p[k];i;i=edge[i].nxt)
    if (edge[i].to!=from&&!flag[edge[i].to])
    {
        dfs(edge[i].to,k);
        size[k]+=size[edge[i].to];
    }
}
void solve(int k)
{
    getsize(k,k);
    flag[k=findroot(k,k,size[k])]=1;
    cnt=0;dfs(k,k);
    memset(f[size[k]+1],0,sizeof(f[size[k]+1]));
    //for (int i=1;i<=n;i++) cout<<size[i]<<' ';cout<<endl;
    //for (int i=1;i<=n;i++) cout<<dfn[i]<<' ';cout<<endl;
    //for (int i=1;i<=n;i++) cout<<id[i]<<' ';cout<<endl;
    //cout<<endl;
    for (int i=size[k];i;i--)
    {
        int x=id[i];
        for (int j=0;j<=m;j++)
        if (j>=c[x]) f[i][j]=f[i+1][j-c[x]]+w[x];
        else f[i][j]=-inf;
        int y=d[x];
        for (int u=0;u<=13;u++)
        if (y)
        {
            int z=min(y,1<<u);
            for (int j=m;j>=c[x]*z;j--)
            f[i][j]=max(f[i][j],f[i][j-c[x]*z]+w[x]*z);
            y-=z;
        }
        for (int j=0;j<=m;j++)
        f[i][j]=max(f[i][j],f[i+size[x]][j]);
    }
    ans=max(ans,f[1][m]);
    for (int i=p[k];i;i=edge[i].nxt)
    if (!flag[edge[i].to]) solve(edge[i].to);
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj4182.in","r",stdin);
    freopen("bzoj4182.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    int T=read();
    while (T--)
    {
        n=read(),m=read();memset(p,0,sizeof(p));t=ans=0;
        for (int i=1;i<=n;i++) w[i]=read();
        for (int i=1;i<=n;i++) c[i]=read();
        for (int i=1;i<=n;i++) d[i]=read()-1;
        for (int i=1;i<n;i++)
        {
            int x=read(),y=read();
            addedge(x,y),addedge(y,x);
        }
        memset(flag,0,sizeof(flag));solve(1);
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2019-01-18 12:11  Gloid  阅读(121)  评论(0编辑  收藏