BZOJ4182 : Shopping

最后选择的一定是树上的一个连通块,考虑树分治,每次只需考虑重心必选的情况,这就变成了以重心为根的树形依赖多重背包问题。

设f[x][j]表示从根节点到x这条路径及其左边的所有节点,以及以x为根的子树的所有节点中,容量为j的背包选取物品所能得到的最大价值。

对于x的儿子y,将f[y]初始值设为f[x]中强制放入一个y,然后将d[y]-1二进制拆分后放入f[y]中,最后将f[x][j]与f[y][j]取个最优解即可。

时间复杂度$O(nm\log n\log d)$。

#include<cstdio>
#define N 510
int T,n,m,i,x,y,ed,g[N],nxt[N<<1],v[N<<1],ok[N<<1],son[N],f[N],size,now;
int a[N],b[N],c[N],dp[N][4010],ans;
inline void add(int x,int y){v[++ed]=y,nxt[ed]=g[x],ok[ed]=1,g[x]=ed;}
inline void up(int&a,int b){if(a<b)a=b;}
void findroot(int x,int y){
  son[x]=1;f[x]=0;
  for(int i=g[x];i;i=nxt[i])if(ok[i]&&v[i]!=y){
    findroot(v[i],x);
    son[x]+=son[v[i]];
    if(son[v[i]]>f[x])f[x]=son[v[i]];
  }
  if(size-son[x]>f[x])f[x]=size-son[x];
  if(f[x]<f[now])now=x;
}
void dfs(int x,int y,int m){
  if(m<=0)return;
  int i,j,k,V,W;
  for(j=c[x],i=0;j;i++)if((1<<i)<=j){
    for(V=a[x]<<i,W=b[x]<<i,k=m;k>=W;k--)up(dp[x][k],dp[x][k-W]+V);
    j-=1<<i;
  }else{
    for(V=a[x]*j,W=b[x]*j,k=m;k>=W;k--)up(dp[x][k],dp[x][k-W]+V);
    break;
  }
  for(i=g[x];i;i=nxt[i])if(ok[i]&&v[i]!=y){
    for(j=0;j<=m-b[v[i]];j++)dp[v[i]][j]=dp[x][j]+a[v[i]];
    dfs(v[i],x,m-b[v[i]]);
    for(j=b[v[i]];j<=m;j++)up(dp[x][j],dp[v[i]][j-b[v[i]]]);
  }
}
void solve(int x){
  int i;
  for(i=0;i<=m-b[x];i++)dp[x][i]=a[x];
  for(dfs(x,i=0,m-b[x]);i<=m-b[x];i++)up(ans,dp[x][i]);
  for(i=g[x];i;i=nxt[i])if(ok[i])ok[i^1]=0,f[0]=size=son[v[i]],findroot(v[i],now=0),solve(now);
}
int main(){
  for(scanf("%d",&T);T--;printf("%d\n",ans)){
    scanf("%d%d",&n,&m),ans=0,ed=1;
    for(i=1;i<=n;i++)scanf("%d",&a[i]),g[i]=0;
    for(i=1;i<=n;i++)scanf("%d",&b[i]);
    for(i=1;i<=n;i++)scanf("%d",&c[i]),c[i]--;
    for(i=1;i<n;i++)scanf("%d%d",&x,&y),add(x,y),add(y,x);
    f[0]=size=n,findroot(1,now=0),solve(now);
  }
  return 0;
}

  

时间: 2024-08-29 10:55:06

BZOJ4182 : Shopping的相关文章

Shopping Offers

Shopping Offers 在商店中,每一种商品都有一个价格(用整数表示).例如,一朵花的价格是 2 zorkmids (z),而一个花瓶的价格是 5z .为了吸引更多的顾客,商店举行了促销活动. 促销活动把一个或多个商品组合起来降价销售,例如: 三朵花的价格是 5z 而不是 6z, 两个花瓶和一朵花的价格是 10z 而不是 12z. 编写一个程序,计算顾客购买一定商品的花费,尽量利用优惠使花费最少.尽管有时候添加其他商品可以获得更少的花费,但是你不能这么做. 对于上面的商品信息,购买三朵花

ZOJ 3524 Crazy Shopping

Crazy Shopping Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 352464-bit integer IO format: %lld      Java class name: Main Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C.P), Kawashiro

ZOJ3524Crazy Shopping(完全背包+拓扑排序)经典

H - Crazy Shopping Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C.P), Kawashiro Nitori decides to buy a lot of rare things

[2016-04-25][codeforces][665B - Shopping]

时间:2016-04-25 20:52:11 星期一 题目编号:[2016-04-25][codeforces][665B - Shopping] 题目大意:已知n个货物的位置,已知每个客人需要的货物,问总共需要走多少次. 分析:直接模拟 #include<cstdio> #include<deque> using namespace std; deque<int> q; deque<int>::iterator itq; int main(){ int n

usaco Shopping Offers(多重完全背包)

有个人要去超时买东西,超市推出了一些优惠信息,每个优惠信息可以使用多次,每条优惠信息限制了要使用这个优惠信息所需购买的物品种类和每种物品的数量. 求买下这个人想买得所有物品总共需要的最少花费. 分析之后以优惠券作为物品,以要买的物品总量为容量,花费的钱为价值. /* ID: modengd1 PROG: shopping LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> #in

[MVC][Shopping]Copy Will&#39;s Code

数据模型规划(Models) 1 //DisplayNameAttribute 指定属性的显示名称 2 [DisplayName("商品类别")] 3 //DisplayColumnAttribute 指定Name为外键列 4 [DisplayColumn("Name")] 5 public class ProductCategory 6 { 7 [Key] 8 public int Id { get; set; } 9 10 [DisplayName("

1044. Shopping in Mars (25)

时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cu

hdu 3264 Open-air shopping malls 求两圆相交

对每个圆二分半径寻找可行的最小半径,然后取最小的一个半径. 对于两圆相交就只要求到两个扇形,然后减去两个全等三角形就行了. #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> using namespace std; #define pi acos(-1.0) #define eps 1e-8 #define maxn 50 int n; struct point{

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam