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 ...
随机推荐
- Java集合框架(set)
set继承自collection接口,其子类和子接口如下: set的共同特性:不能添加相同的元素,通常无法记住元素添加的顺序 1.HashSet类 判断两元素相同的标准:1.equals方法返回tru ...
- fmt:formatNumber use locale display negative currency in -$xxx.xx format in JSTL
First, we want to know our own locale,how to display the locale in a JSTL? <c:out value="${p ...
- Jquery validate验证表单时多个name相同的元素只验证第一个的问题
下面搜集了五种方法,主要还是前两个提供了解决方案,第三种需要修改jQuery源码: 修复jquery.validate插件中name属性相同(如name='a[]')时验证的bug 使用jquery. ...
- Oracl闪回数据命令。
当数据库操作没有备份,并且误删数据.可闪回任何 当前闪回15分钟前数据库状态. alter table BASE_APPOINT_LOG enable row movement;flashback ...
- 【BZOJ】4872: [Shoi2017]分手是祝愿 期望DP
[题意]给定n盏灯的01状态,操作第 i 盏灯会将所有编号为 i 的约数的灯取反.每次随机操作一盏灯直至当前状态能够在k步内全灭为止(然后直接灭),求期望步数.n,k<=10^5. [算法]期望 ...
- 浅谈卡特兰数(Catalan number)的原理和相关应用
一.卡特兰数(Catalan number) 1.定义 组合数学中一个常出现在各种计数问题中出现的数列(用c表示).以比利时的数学家欧仁·查理·卡特兰的名字来命名: 2.计算公式 (1)递推公式 c[ ...
- 36 - 网络编程-TCP编程
目录 1 概述 2 TCP/IP协议基础 3 TCP编程 3.1 通信流程 3.2 构建服务端 3.3 构建客户端 3.4 常用方法 3.4.1 makefile方法 3.5 socket交互 3.4 ...
- Fedora8 U盘安装
(一)分区 在XP下"我的电脑“管理功能,对硬盘分区,目的是从逻辑分区中拿出20G空间,分成3个盘(必须为逻辑盘): (1)512MB 用作Linux swap分区: (2)200MB ...
- 牛B的日本精神
在汤森路透评选出的<2015全球创新企业百强>榜单里,日本以40家高居榜首,力压美国的35家.而中国内地无一入围. 在中国媒体上,我们见到的日本是“失去的20年”,经济衰退.创新能力丧 ...
- 在ubuntu中安装puppeteer
https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md 早些时候puppeteer刚出来,在vps上 ...