题目传送门

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?

Input

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

Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.

Examples
input

Copy
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
output

Copy
4
input

Copy
5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
output

Copy
5
Note

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]的更多相关文章

  1. Codeforces 815C Karen and Supermarket 树形dp

    Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...

  2. 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 ...

  3. 816E. Karen and Supermarket 树形DP

    LINK 题意:给出n个商品,除第一个商品外,所有商品可以选择使用优惠券,但要求其前驱商品已被购买,问消费k以下能买几个不同的商品 思路:题意很明显就是树形DP.对于一个商品有三种选择,买且使用优惠券 ...

  4. CF815C Karen and Supermarket

    题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...

  5. [CF816E] Karen and Supermarket1 [树形dp]

    传送门 - > \(CF816E\) Karen and Supermarket 题意翻译 在回家的路上,凯伦决定到超市停下来买一些杂货. 她需要买很多东西,但因为她是学生,所以她的预算仍然很有 ...

  6. Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  7. codeforces 816 E. Karen and Supermarket(树形dp)

    题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...

  8. CodeForces 816E Karen and Supermarket ——(树形DP)

    题意:有n件商品,每件商品都最多只能被买一次,且有一个原价和一个如果使用优惠券以后可以减少的价格,同时,除了第一件商品以外每件商品都有一个xi属性,表示买这个商品时如果要使用优惠券必须已经使用了xi的 ...

  9. E. Karen and Supermarket

    E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

随机推荐

  1. 51 nod 1109 01组成的N的倍数

    1109 01组成的N的倍数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且 ...

  2. [转]memmove、memcpy和memccpy

    原文地址:http://www.cppblog.com/kang/archive/2009/04/05/78984.html 在原文基础上进行了一些小修改~ memmove.memcpy和memccp ...

  3. laravel 重定向路由带参数

    转载: http://www.cnblogs.com/foreversun/p/5642176.html 举例: 路由: //任务列表页 $router->get('/taskDetail/{i ...

  4. [ JS 进阶 ] 闭包,作用域链,垃圾回收,内存泄露

    原网址:https://segmentfault.com/a/1190000002778015 1. 什么是闭包? 来看一些关于闭包的定义: 闭包是指有权访问另一个函数作用域中变量的函数 --< ...

  5. filezilla显示隐藏文件

    我们在习惯使用flashfxp等工具,但是随着主机商限制较多,这些老的FTP工具不怎么好用了,比如主机商会推荐使用到Filezilla等工具.但是比如息壤主机,我们在管理linux环境下htacess ...

  6. Android SDK的安装与环境变量的配置

    配置Andriod环境变量前提是要先安装好JAVA环境 1.下载Android SDK,点击安装,放在任意不含空格.特殊符号和中文的路径即可. 2.默认路径安装后,安装完成,开始配置环境变量. 3.打 ...

  7. Mac最新系统bssdb BUG

    这个bug在Mac OS更新到10.14时候出现,当前系统版本 ➜ git:(master) sw_vers ProductName: Mac OS X ProductVersion: 10.14 B ...

  8. arch点击硬盘无法挂载

    出现问题如下 在使用xfce4桌面的时候在点击硬盘图标时可以挂载虽然要求你输入root密码 但是在使用openbox的时候点击硬盘图标却出现如下提示,权限的问题 Not authorized to p ...

  9. weblogic12.1.3 静默安装 建域

    --安装依赖包 yum -y install compat-libcap1 compat-libstdc++ gcc gcc-c++ glibc-devel libaio-devel libstdc+ ...

  10. 正排索引(forward index)与倒排索引(inverted index) (转)

    一.正排索引(前向索引) 正排索引也称为"前向索引".它是创建倒排索引的基础,具有以下字段. (1)LocalId字段(表中简称"Lid"):表示一个文档的局部 ...