CF815C Karen and Supermarket [树形DP]
Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries.

She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars.
The supermarket sells n goods. The i-th good can be bought for ci dollars. Of course, each good can only be bought once.
Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given n coupons. If Karen purchases the i-th good, she can use the i-th coupon to decrease its price by di. Of course, a coupon cannot be used without buying the corresponding good.
There is, however, a constraint with the coupons. For all i ≥ 2, in order to use the i-th coupon, Karen must also use the xi-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).
Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget b?
The first line of input contains two integers n and b (1 ≤ n ≤ 5000, 1 ≤ b ≤ 109), the number of goods in the store and the amount of money Karen has, respectively.
The next n lines describe the items. Specifically:
- The i-th line among these starts with two integers, ci and di (1 ≤ di < ci ≤ 109), the price of the i-th good and the discount when using the coupon for the i-th good, respectively.
- If i ≥ 2, this is followed by another integer, xi (1 ≤ xi < i), denoting that the xi-th coupon must also be used before this coupon can be used.
Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
4
5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
5
In the first test case, Karen can purchase the following 4 items:
- Use the first coupon to buy the first item for 10 - 9 = 1 dollar.
- Use the third coupon to buy the third item for 12 - 2 = 10 dollars.
- Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars.
- Buy the sixth item for 2 dollars.
The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.
In the second test case, Karen has enough money to use all the coupons and purchase everything.
分析:
考试的时候遇到这道题结果还是一脸懵逼,果然我$DP$还是太弱了,还是得好好补补。
考虑用树形$DP$,根据题意构建出一棵树,然后从根节点开始深搜,把每个节点遍历一遍,然后从叶子节点开始向根节点转移状态。
定义$f[x][j][0/1]$表示遍历到了第$x$个商品时已经购买了$j$个商品,$0/1$表示第$x$个商品是否打折。
那么状态转移方程为:
$f[x][j+k][0]=Min(f[x][j+k][0],f[x][j][0]+f[y][k][0]);$
$f[x][j+k][1]=Min(f[x][j+k][1],f[x][j][1]+f[y][k][0]);$
$f[x][j+k][1]=Min(f[x][j+k][1],f[x][j][1]+f[y][k][1]);$
其中$j$和$k$分别表示当前节点和其子节点已经搜过的子树中购买的商品个数。可能有点绕,可以结合代码理解。
Code:
//It is made by HolseLee on 15th Aug 2018
//CF815C
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define Max(a,b) (a)>(b)?(a):(b)
#define Min(a,b) (a)<(b)?(a):(b)
#define Swap(a,b) (a)^=(b)^=(a)^=(b)
using namespace std; const int N=;
int n,m,head[N],siz,w[N],c[N],rt[N];
long long f[N][N][];
struct Node{
int to,nxt;
}edge[N<<]; inline int read()
{
char ch=getchar();int num=;bool flag=false;
while(ch<''||ch>''){
if(ch=='-')flag=true;ch=getchar();
}
while(ch>=''&&ch<=''){
num=num*+ch-'';ch=getchar();
}
return flag?-num:num;
} inline void add(int x,int y)
{
edge[++siz].to=y;
edge[siz].nxt=head[x];
head[x]=siz;
} void dfs(int x)
{
rt[x]=;f[x][][]=;
f[x][][]=w[x];f[x][][]=c[x];
for(int i=head[x];i;i=edge[i].nxt){
int y=edge[i].to;
dfs(y);
for(int j=rt[x];j>=;--j)
for(int k=;k<=rt[y];++k){
f[x][j+k][]=Min(f[x][j+k][],f[x][j][]+f[y][k][]);
f[x][j+k][]=Min(f[x][j+k][],f[x][j][]+f[y][k][]);
f[x][j+k][]=Min(f[x][j+k][],f[x][j][]+f[y][k][]);
}
rt[x]+=rt[y];
}
} int main()
{
//freopen("shopping.in","r",stdin);
//freopen("shopping.out","w",stdout);
n=read();m=read();
memset(f,/,sizeof(f));
w[]=read();c[]=read();c[]=w[]-c[];
int x,y,z;
for(int i=;i<=n;++i){
x=read();y=read();z=read();
w[i]=x;c[i]=x-y;
add(z,i);
}
dfs();
for(int i=n;i>=;--i){
if(f[][i][]<=m||f[][i][]<=m){
printf("%d\n",i);
break;
}
}
return ;
}
CF815C Karen and Supermarket [树形DP]的更多相关文章
- Codeforces 815C Karen and Supermarket 树形dp
Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...
- Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP
C. Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some g ...
- 816E. Karen and Supermarket 树形DP
LINK 题意:给出n个商品,除第一个商品外,所有商品可以选择使用优惠券,但要求其前驱商品已被购买,问消费k以下能买几个不同的商品 思路:题意很明显就是树形DP.对于一个商品有三种选择,买且使用优惠券 ...
- CF815C Karen and Supermarket
题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...
- [CF816E] Karen and Supermarket1 [树形dp]
传送门 - > \(CF816E\) Karen and Supermarket 题意翻译 在回家的路上,凯伦决定到超市停下来买一些杂货. 她需要买很多东西,但因为她是学生,所以她的预算仍然很有 ...
- Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)
http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...
- codeforces 816 E. Karen and Supermarket(树形dp)
题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...
- CodeForces 816E Karen and Supermarket ——(树形DP)
题意:有n件商品,每件商品都最多只能被买一次,且有一个原价和一个如果使用优惠券以后可以减少的价格,同时,除了第一件商品以外每件商品都有一个xi属性,表示买这个商品时如果要使用优惠券必须已经使用了xi的 ...
- E. Karen and Supermarket
E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
随机推荐
- springboot创建maven多模块项目
SpringBoot创建maven多模块项目 项目结构 该项目名称为springboot-maven-multi,由springboot-maven-multi.user-dao.user-domai ...
- SpringCloud学习(2)——Rest微服务案例
创建父工程: microservicecloud 创建公共模块api:microservicecloudapi SQL脚本: 此学习路线总共创建3个库, 分别为clouddb01, clouddb0 ...
- 字符串:KMP
KMP是字符串匹配的经典算法 也是众多字符串基础的重中之重 A. 题意:给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出最先匹配完成的位置,否则输出-1. 做法:直 ...
- CF851 C 暴力
给出n个5维下的点,求点a不与其它任意的b,c重合,向量ab,ac的夹角都为钝角,这样的点个数,并打印它们. 转换二维下的求角度的函数为五维的,而且由于要求角度大于90度,在二维情况下最多有4个点,也 ...
- PHP与Ajax
如何用PHP接收JSON格式数据 1.一般来说,我们直接用$_POST $_REQUEST $_GET这样的超全局变量接收就好了 <?php $obj_temp=$_POST['data']; ...
- ELK 企业内部日志分析系统
生产环境配置 亿级规模,建议64G内存+8核CPU ES JVM占用一半内存 生产环境的3节点的集群 https://blog.csdn.net/xuduorui/article/details/79 ...
- Redis实战(五)CentOS 7上搭建Redis集群
高可用Redis(十二):Redis Cluster https://www.cnblogs.com/renpingsheng/p/9862485.html https://www.cnblogs.c ...
- 【BZOJ】4753: [Jsoi2016]最佳团体 01分数规划+树上背包
[题意]n个人,每个人有价值ai和代价bi和一个依赖对象ri<i,选择 i 时 ri 也必须选择(ri=0时不依赖),求选择k个人使得Σai/Σbi最大.n<=2500,ai,bi< ...
- 详细说说如何生成验证码—ASP.NET细枝末节(4)
前言 今天小编详细的说一下,ASP.NET网站开发过程中生成验证码的全部问题. 本文的目标,是让读者了解,生成验证码涉及的全部基础知识问题. 当然这里说的是比较简单的验证码. 真正符合要求的验证码,涉 ...
- 母版页 VS shtml—ASP.NET细枝末节(3)
这算是html的重用吧? 网页很多地方长得一样,也有不一样的地方. 把网页中一样的地方,提取出来,形成一个文档. 在其他网页中引用,是网站开发的一个传统的思维. 当然不同的技术有不同的表现形式. 例如 ...