# 【bzoj4182】Shopping 树的点分治+dfs序+背包dp

$n\le 500,m\le 4000$

1
3 2
1 2 3
1 1 1
1 2 1
1 2
1 3

4

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 510
using namespace std;
int m , w[N] , c[N] , d[N] , head[N] , to[N << 1] , next[N << 1] , cnt , si[N] , ms[N] , sum , root , vis[N] , val[N] , last[N] , tot , f[N][4010] , ans;
inline void add(int x , int y)
{
}
void getroot(int x , int fa)
{
int i;
si[x] = 1 , ms[x] = 0;
for(i = head[x] ; i ; i = next[i])
if(!vis[to[i]] && to[i] != fa)
getroot(to[i] , x) , si[x] += si[to[i]] , ms[x] = max(ms[x] , si[to[i]]);
ms[x] = max(ms[x] , sum - si[x]);
if(ms[x] < ms[root]) root = x;
}
void dfs(int x , int fa)
{
int i;
si[x] = 1 , val[++tot] = x;
for(i = head[x] ; i ; i = next[i])
if(!vis[to[i]] && to[i] != fa)
dfs(to[i] , x) , si[x] += si[to[i]];
last[x] = tot;
}
void solve(int x)
{
int i , j , k , t;
vis[x] = 1 , tot = 0 , dfs(x , 0);
for(i = 1 ; i <= tot + 1 ; i ++ )
for(j = 0 ; j <= m ; j ++ )
f[i][j] = 0;
for(i = tot ; i ; i -- )
{
t = d[val[i]] - 1;
for(j = m ; j >= c[val[i]] ; j -- ) f[i][j] = f[i + 1][j - c[val[i]]] + w[val[i]];
for(j = 1 ; j <= t ; t -= j , j <<= 1)
for(k = m ; k >= j * c[val[i]] ; k -- )
f[i][k] = max(f[i][k] , f[i][k - j * c[val[i]]] + j * w[val[i]]);
if(t)
for(j = m ; j >= t * c[val[i]] ; j -- )
f[i][j] = max(f[i][j] , f[i][j - t * c[val[i]]] + t * w[val[i]]);
for(j = m ; ~j ; j -- ) f[i][j] = max(f[i][j] , f[last[val[i]] + 1][j]);
}
ans = max(ans , f[1][m]);
for(i = head[x] ; i ; i = next[i])
if(!vis[to[i]])
sum = si[to[i]] , root = 0 , getroot(to[i] , 0) , solve(root);
}
int main()
{
int T;
scanf("%d" , &T);
while(T -- )
{
memset(vis , 0 , sizeof(vis)) , ans = 0;
int n , i , x , y;
scanf("%d%d" , &n , &m);
for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &w[i]);
for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &c[i]);
for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &d[i]);
for(i = 1 ; i < n ; i ++ ) scanf("%d%d" , &x , &y) , add(x , y) , add(y , x);
sum = n , ms[0] = 1 << 30 , root = 0 , getroot(1 , 0) , solve(root);
printf("%d\n" , ans);
}
return 0;
}


posted @ 2018-03-18 17:19  GXZlegend  阅读(779)  评论(0编辑  收藏