BZOJ1017 魔兽地图DotR (树上背包)
一道背包的神题,用到了树上dp和背包dp,这个题的特殊性在于儿子对于父亲节点是有影响的,所以用f[i][j][k]表示第i号装备,其中用j个来合成上层装备,花费k元所能获得最大的力量值。
然后对于每一个节点枚举我选择合成几个,遍历每一个儿子节点,背包dp一下花费k元的最大力量值。注意这里的背包是一个分组背包,即对于每一个节点,我需要选择它的每一个叶子节点,这里每一个叶子都是一组物品(因为我需要枚举给每个叶子的花费),我需要选择每一组里的一个物品,所以是一个分组背包,最后用算出背包的g数组去更新f数组,同样是枚举话多少钱,把几个物品用于上层合成,然后转移状态。
——by VANE
#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-f;ch=getchar();}
while(ch>=''&&ch<='') {x=x*-''+ch;ch=getchar();}
return f*x;
}
int n,m,ans,TOT,cnt;
int P[],L[],M[];
int f[][][];
int g[],h[];
char ch[];
int last[],deg[];
struct date{int to,next,v;}e[];
void insert(int u,int v,int w)
{
e[++cnt].to=v;e[cnt].next=last[u];
last[u]=cnt;e[cnt].v=w;deg[v]++;
}
void dp(int x)
{
if(!last[x])
{
L[x]=min(L[x],m/M[x]);
for(int i=;i<=L[x];++i)
for(int j=i;j<=L[x];++j)
f[x][i][j*M[x]]=(j-i)*P[x];
return;
}
L[x]=inf;
for(int i=last[x];i;i=e[i].next)
{
dp(e[i].to);
L[x]=min(L[x],L[e[i].to]/e[i].v);
M[x]+=e[i].v*M[e[i].to];
}
L[x]=min(L[x],m/M[x]); for(int l=L[x];l>=;--l)
{
memset(g,-0x3f3f3f3f,sizeof g);g[]=;
for(int i=last[x];i;i=e[i].next)
{
for(int j=m;j>=;--j)
{
int t=-1e9;
for(int k=;k<=j;++k)
t=max(t,g[j-k]+f[e[i].to][l*e[i].v][k]);
g[j]=t;
} }
for(int j=;j<=l;++j)
for(int k=;k<=m;++k)
f[x][j][k]=max(f[x][j][k],g[k]+P[x]*(l-j));
}
}
int main()
{
memset(f,-0x3f3f3f3f,sizeof f);
n=read();m=read();
for(int i=;i<=n;++i)
{
P[i]=read();
scanf("%s",ch);
if(ch[]=='A')
{
int x=read();
while(x--)
{
int v=read(),num=read();
insert(i,v,num);
}
}
else M[i]=read(),L[i]=read();
}
for(int x=;x<=n;++x)
if(!deg[x])
{
dp(x);
for(int i=m;i>=;--i)
for(int j=;j<=i;++j)
h[i]=max(h[i],h[j]+f[x][][i-j]);
}
cout<<h[m];
}
BZOJ1017 魔兽地图DotR (树上背包)的更多相关文章
- BZOJ1017魔兽地图DotR 樹形DP
@(BZOJ)[樹形DP, 三維DP] Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA ...
- 【BZOJ-1017】魔兽地图DotR 树形DP + 背包
1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1566 Solved: 705[Submit][S ...
- 【bzoj1017】[JSOI2008]魔兽地图DotR
1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1658 Solved: 755[Submit][S ...
- [BZOJ1017][JSOI2008]魔兽地图DotR 树形dp
1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2597 Solved: 1010[Submit][ ...
- BZOJ [JSOI2008]魔兽地图DotR
1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1243 Solved: 532[Submit][S ...
- [bzoj1017][JSOI2008]魔兽地图 DotR (Tree DP)【有待优化】
Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Anc ...
- BZOJ1017: [JSOI2008]魔兽地图DotR【树形DP】【玄学】
Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Anc ...
- bzoj 1017: [JSOI2008]魔兽地图DotR【树形dp+背包】
bzoj上是一个森林啊--? dp还是太弱了 设f[i][j][k]为到点i,合成j个i并且花费k金币能获得的最大力量值,a[i]为数量上限,b[i]为价格,p[i]为装备力量值 其实这个状态设计出来 ...
- 1017: [JSOI2008]魔兽地图DotR - BZOJ
Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Anc ...
随机推荐
- composer 上提交自己的包
先在github上复制自己的地址在 https://packagist.org/packages/submit ->check->submit
- centOS7 vsftp ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS) 启动失败问题?
[root@localhost c]# systemctl status vsftpd.service ● vsftpd.service - Vsftpd ftp daemon Loaded: loa ...
- 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
- Redis数据类型之散列(hash)
1. 什么是散列 散列类似于一个字典,是一个<K, V>对的集合,不过这个key和value都只能是字符串类型的,不能嵌套,可以看做Java中的Map<String, String& ...
- zedboard学习记录.1.纯PL流水灯
环境:vivado 217.4 开发板: zedboard ver.d xc7z020clg484-1 1.打开Vivado新建一个RTL工程. 2.add source->add/create ...
- PHP动态修改配置文件——php经典实例
文件结构: index.php 主页 config 配置文件 doUpdate.php 修改功能页 index.php <html> <head> <title>修 ...
- Linux 编译 apr-util 时报错
前言 Apache 2.4 以后的版本不再自带 APR 库(Apache Portable Runtime,Apache 可移植运行库),所以在安装 Apache 之前需要手动下载安装 APR 库. ...
- Bootstrap的使用。。。
概览 深入了解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML5 文档类型 Bootstrap 使用到的某些 HTML 元素和 CSS 属 ...
- ASP .NET CORE MVC 部署Windows 系统上 IIS具体步骤---.Net Core 部署到 IIS位系统中的步骤
一.IIS 配置 启用 Web 服务器 (IIS) 角色并建立角色服务. 1.Windows Ddesktop 桌面操作系统(win7及更高版本) 导航到“控制面板” > “程序” > “ ...
- 720P、1080P、4K是什么意思?
什么是像素? 中文全称为图像元素.像素仅仅只是分辨率的尺寸单位,而不是画质. 从定义上来看,像素是指基本原色素及其灰度的基本编码. 像素是构成数码影像的基本单元,通常以像素每英寸PPI(pixels ...