[BZOJ4182]Shopping

description

权限题。
树上\(n\)个节点每个节点都有一种物品,每种物品有其价值,价格,数量,只能买一个连通块中的物品,求\(m\)元能买到物品价值的最大值。

data range

\[ n\le 500,m\le 4000,T\le 5,c_i\le m\]

solution

紧跟\(YCB\)聚聚的步伐

首先可以想到以每个点为根做树形依赖背包

树形依赖背包

\(f[i][j]\)表示在子树\(i\)中买了价值为\(j\)的物品

如果直接对父亲转移是每次\(O(nc^2)\)

我们把每个连通块考虑成一条路径,如果选某个点就前往这个点\(dfn\)序的下一个点,
如果不选就跳过整棵子树做转移

这样做再加上多重背包的单调队列优化可以达到每次\(O(nc)\)

总复杂度为\(O(n^2c)\)

然后套一下点分治或者\(dsu\ on\ tree\)都可以

code

#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<complex>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define FILE "a"
#define mp make_pair
#define pb push_back
#define RG register
#define il inline
using namespace std;
typedef unsigned long long ull;
typedef vector<int>VI;
typedef long long ll;
typedef double dd;
const dd eps=1e-10;
const int mod=1e9+7;
const int N=510;
const int M=4010;
const dd pi=acos(-1);
const int inf=mod;
const ll INF=1e18+1;
const ll P=100000;
il ll read(){RG ll data=0,w=1;RG char ch=getchar();while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();if(ch=='-')w=-1,ch=getchar();while(ch<='9'&&ch>='0')data=data*10+ch-48,ch=getchar();return data*w;
}il void file(){srand(time(NULL)+rand());freopen(FILE".in","r",stdin);freopen(FILE".out","w",stdout);
}int n,m,w[N],c[N],d[N],ans;
int head[N],nxt[N<<1],to[N<<1],cnt;
int sz[N],son[N],dfn[N],fw[N],cntw;
void dfs1(int u,int fa){sz[u]=1;son[u]=0;for(RG int i=head[u];i;i=nxt[i]){RG int v=to[i];if(v==fa)continue;dfs1(v,u);sz[u]+=sz[v];if(sz[son[u]]<sz[v])son[u]=v;}
}
void dfs2(int u,int fa){dfn[u]=++cntw;fw[cntw]=u;for(RG int i=head[u];i;i=nxt[i]){RG int v=to[i];if(v==fa||v==son[u])continue;dfs2(v,u);}if(son[u])dfs2(son[u],u);
}int dp[N][M],q[M],l,r;
il void upd(int &a,int b){a=a>b?a:b;}
il void work(int *f1,int *f2,int u){for(RG int i=0,ret;i<c[u];i++){l=1;r=0;for(RG int j=0;j*c[u]+i<=m;j++){while(l<=r&&j-q[l]>d[u])l++;ret=l>r?-inf:f2[q[l]*c[u]+i]+(j-q[l])*w[u];upd(f1[j*c[u]+i],ret);while(l<=r&&f2[q[r]*c[u]+i]<=f2[j*c[u]+i]+(q[r]-j)*w[u])r--;q[++r]=j;}}
}void dsu(int u,int fa,int k){for(RG int i=head[u];i;i=nxt[i]){RG int v=to[i];if(v==fa||v==son[u])continue;dsu(v,u,0);}if(son[u])dsu(son[u],u,1);for(RG int i=0;i<=m;i++)dp[fw[dfn[u]+sz[u]]][i]=-inf;dp[fw[dfn[u]+sz[u]]][0]=0;for(RG int i=dfn[u]+sz[u]-sz[son[u]]-1;i>=dfn[u];i--){RG int x=fw[i];for(RG int j=0;j<=m;j++)dp[x][j]=-inf;if(x!=u)for(RG int j=0;j<=m;j++)upd(dp[x][j],dp[fw[i+sz[x]]][j]);work(dp[x],dp[fw[i+1]],x);}for(RG int i=0;i<=m;i++)upd(ans,dp[u][i]);if(k){for(RG int i=0;i<=m;i++)dp[u][i]=-inf;for(RG int i=0;i<=m;i++)upd(dp[u][i],dp[fw[dfn[u]+sz[u]]][i]);work(dp[u],dp[fw[dfn[u]+1]],u);}
}int main()
{RG int T=read();while(T--){n=read();m=read();ans=-inf;fw[n+1]=n+1;cnt=cntw=0;memset(head,0,sizeof(head));for(RG int i=1;i<=n;i++)w[i]=read();for(RG int i=1;i<=n;i++)c[i]=read();for(RG int i=1;i<=n;i++)d[i]=read();for(RG int i=1,u,v;i<n;i++){u=read();v=read();to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;to[++cnt]=u;nxt[cnt]=head[v];head[v]=cnt;}       dfs1(1,0);dfs2(1,0);dsu(1,0,0);     printf("%d\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/cjfdf/p/9540057.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279521.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何用 Flutter 实现混合开发?闲鱼公开源代码实例

2019独角兽企业重金招聘Python工程师标准>>> 具有一定规模的 App 通常有一套成熟通用的基础库&#xff0c;尤其是阿里系 App&#xff0c;一般需要依赖很多体系内的基础库。那么使用 Flutter 重新从头开发 App 的成本和风险都较高。所以在 Native App 进行渐进式迁移…

Silverlight之工具箱使用1

我们在开发Silverlight项目时必定需要使用VS自带的一些控件&#xff0c;但是这些有限的控件有时候难以满足开发时的需求&#xff0c;因此MS给我们大家提供另外一套工具&#xff0c;来缓解Silverlight开发包的不足。此工具箱免费下载地址是&#xff1a;http://silverlight.codep…

apple tv设置_如何设置Apple HomePod

apple tv设置Apple’s HomePod smart speaker is finally here. If you bought one and are eager to get going, here’s how to set it up. 苹果的HomePod智能扬声器终于来了。 如果您购买了一个并且渴望上手&#xff0c;请按照以下步骤进行设置。 First off, before you eve…

leetcode 128最长连续序列

方法一&#xff1a;使用快排&#xff1a; //排序法&#xff0c;时间O(nlogn)&#xff0c;使用STL&#xff0c;只是验证一下思想&#xff0c;非正解&#xff1b; class Solution { public:int longestConsecutive(vector<int>& nums) {sort(nums.begin(),nums.end());…

8月19学习练习[两三个TableView并排显示]

要求&#xff1a;在一个view中显示两个tableView&#xff0c;要求左右显示的内容以及行数不一样&#xff0c;且左边每行显示两张图片&#xff08;分别3个一轮回&#xff0c;2个一轮回&#xff09;并且显示中国的城市名&#xff0c;右边显示水果名。点击时分别显示城市名或水果名…

word多级列表创建目录_如何在Microsoft Word中创建和使用多级列表

word多级列表创建目录Microsoft Word lets you easily create and format multilevel lists in your documents. You can choose from a variety of formatting options, including bulleted, numbered, or alphabetized lists. Let’s take a look. Microsoft Word使您可以轻松…

如何将多个Android Wear手表与单个手机配对

When it comes to “regular” wristwatches, a lot of people have different watches for different activities. It makes sense—a sporty watch for the gym, a nicer watch for the office, and a casual watch for everything else. If you want to live this life with…

Android系统的智能指针(轻量级指针、强指针和弱指针)的实现原理分析(3)...

提供引用计数器的类RefBase我们就暂时介绍到这里&#xff0c;后面我们再结合智能指针类一起分析&#xff0c;现在先来看看强指针类和弱指针类的定义。强指针类的定义我们在前面介绍轻量级指针的时候已经见到了&#xff0c;就是sp类了&#xff0c;这里就不再把它的代码列出来了。…

ref:下一个项目为什么要用 SLF4J

ref:http://blog.mayongfa.cn/267.html 阿里巴巴 Java 开发手册 前几天阿里巴巴在云栖社区首次公开阿里官方Java代码规范标准&#xff0c;就是一个PDF手册&#xff0c;有命名规范&#xff0c;让你知道自己原来取的每一个类名、变量名都是烂名字&#xff0c;真替你家未来孩子担心…

洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)

题面 传送门 题解 日常敲板子.jpg //minamoto #include<bits/stdc.h> #define R register #define inline __inline__ __attribute__((always_inline)) #define fp(i,a,b) for(R int i(a),I(b)1;i<I;i) #define fd(i,a,b) for(R int i(a),I(b)-1;i>I;--i) #define …

计算机突然蓝屏无法启动_为什么计算机无法立即启动?

计算机突然蓝屏无法启动With the newer, more powerful hardware and improved operating systems that we have available to use these days, why does it still take as long as it does to fully boot a computer up each time? 借助我们如今可以使用的更新&#xff0c;更…

CCNA课堂练习:OSPF的介绍及配置

CCNA浅谈OSPF的配置 今天我们来谈谈路由器OSPF的配置&#xff0c;那我先来介绍一下OSPF的特点&#xff1a;1、对网络发生的变化能够快速响应2、当网络发生变化的时候发送触发式更新•3、支持VLAN 4、管理方便ospf引用了区域的概念&#xff0c;区域分两种&#xff1a;1、骨干区域…

vcenter 6.7 (vcsa)部署指南

闲言少叙&#xff0c;直达心灵。 一、部署提要1.1 vCenter Server Appliance(VCSA )6.7下载地址https://pan.baidu.com/s/1WUShsC23E2qIIBg7MPR87w 6lzb 二、安装部署VCSA分为两个阶段安装&#xff0c;下面我们开始第一阶段2.1 打开之后&#xff0c;直接点击安装按钮2.2部署设备…

如何停止Internet Explorer 11的建议站点?

Internet Explorer automatically suggests addresses and search results based on the partial address you’re typing out. If this feature irritates you, read on as we learn how to turn it off. Internet Explorer会根据您键入的部分地址自动建议地址和搜索结果。 如…

什么是SG?+SG模板

先&#xff0c;定义一下 状态Position P 先手必败 N x先手必胜 操作方法&#xff1a; 反向转移 相同状态 不同位置 的一对 相当于无 对于ICG游戏&#xff0c;我们可以将游戏中每一个可能发生的局面表示为一个点。并且若存在局面i和局面j&#xff0c;且j是i的后继局面(即局面i可…

【桌面虚拟化】之三 Persistent vs NonP

作者&#xff1a;范军 &#xff08;Frank Fan&#xff09; 新浪微博&#xff1a;frankfan7 在【桌面虚拟化】之二类型及案例中我们探讨了桌面虚拟化的两种架构&#xff0c;HostedVirtual Desktop (VDI) 和 Published Desktop/App. 本文深入分析其中VDI的两种桌面类型&#xff0…

H5 video 开发问题及相关知识点

相关链接&#xff1a;H5 video 的使用H5 video 全屏播放♢ video点播与直播H5 video目前所有浏览器都支持的视频格式是MP4格式&#xff0c;所以mp4应当是点播web视频的首选格式。而在直播视频上&#xff0c;H5 video只在移动端原生支持HLS流的直播视频(Mac safari video标签也支…

Mybatis-Generator自动生成XML文件以及接口和实体类

整合了MySQL和Oracle配置文件生成方法 这个是整个文件夹的下载地址&#xff1a;http://www.codepeople.cn/download 主要给大家介绍一下generatorConfig.xml文件的配置&#xff0c;以及生成后的文件。 generatorConfig.xml <?xml version"1.0" encoding"UTF…

如何在Windows 10上设置默认Linux发行版

Windows 10 now allows you to install multiple Linux environments, starting with the Fall Creators Update. If you have multiple Linux environments, you can set your default and switch between them. Windows 10现在允许您从Fall Creators Update开始安装多个Linux…

mysql全备份+增量备份笔记总结

备份基础知识 冷备&#xff08;cold backup&#xff09;&#xff1a;需要关mysql服务&#xff0c;读写请求均不允许状态下进行&#xff1b; 温备&#xff08;warm backup&#xff09;&#xff1a; 服务在线&#xff0c;但仅支持读请求&#xff0c;不允许写请求&#xff1b; 热备…