codeforces round #419 E. 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.
题解:
树上背包哈,F[x][j][0/1]表示x子节点和本身中,选j个,当前节点是否打折(0/1)
方程式:
F[x][j+k][0]=min(F[x][j+k][0],F[u][k][0]+F[x][j][0])
F[x][j+k][1]=min(F[x][j+k][1],F[u][k][1]+F[x][j][1])
F[x][j+k][1]=min(F[x][j+k][1],F[u][k][0]+F[x][j][1])
复杂度O(n^2).
注意初始化和边界调节:
F[x][0][0]是要赋为0的,因为当前节点不打折时是可以不选的,而F[x][0][1]不能.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=;
int n,m;
int head[N],num=,w[N],h[N],now[N];ll F[N][N][];
struct Lin
{
int next,to;
} a[N<<];
void init(int x,int y)
{
a[++num].next=head[x];
a[num].to=y;
head[x]=num;
}
void dfs(int x)
{
int u;
now[x]=;
F[x][][]=;
F[x][][]=w[x];F[x][][]=h[x];
for(int i=head[x]; i; i=a[i].next)
{
u=a[i].to;
dfs(u);
for(int j=now[x];j>=;j--)
{
for(int k=; k<=now[u]; k++)
{
F[x][j+k][]=min(F[x][j+k][],F[u][k][]+F[x][j][]);
F[x][j+k][]=min(F[x][j+k][],F[u][k][]+F[x][j][]);
F[x][j+k][]=min(F[x][j+k][],F[u][k][]+F[x][j][]);
}
}
now[x]+=now[u];//now为当前节点的子节点个数.
}
}
int main()
{
scanf("%d%d",&n,&m);
int x,y,fa;
scanf("%d%d",&w[],&h[]);
h[]=w[]-h[];
memset(F,/,sizeof(F));
for(int i=; i<=n; i++)
{
scanf("%d%d%d",&x,&y,&fa);
w[i]=x;
h[i]=x-y;
init(fa,i);
}
dfs();
for(int i=n; i>=; i--)
if(F[][i][]<=m || F[][i][]<=m)
{
printf("%d",i);
break;
}
return ;
}
codeforces round #419 E. Karen and Supermarket的更多相关文章
- Codeforces Round #419 D. Karen and Test
Karen has just arrived at school, and she has a math test today! The test is about basic addition an ...
- codeforces round #419 C. Karen and Game
C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- codeforces round #419 B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- codeforces round #419 A. Karen and Morning
Karen is getting ready for a new school day! It is currently hh:mm, given in a 24-hour format. As yo ...
- 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 ...
- Codeforces Round #419 (Div. 2) A-E
上紫啦! E题1:59压哨提交成功翻盘 (1:00就做完了调了一个小时,还好意思说出来? (逃)) 题面太长就不复制了,但是配图很可爱所以要贴过来 九条可怜酱好可爱呀 A - Karen and Mo ...
- Codeforces round 419 div2 补题 CF 816 A-E
A Karen and Morning 水题 注意进位即可 #include<bits/stdc++.h> using namespace std; typedef long long i ...
- Codeforces Round #419
A Karen and Morning 找最近的回文时间 模拟 往后推 判判就行 //By SiriusRen #include <bits/stdc++.h> using namesp ...
- Codeforces Round #419 (Div. 1) (ABCD)
1. 815A Karen and Game 大意: 给定$nm$矩阵, 每次选择一行或一列全部减$1$, 求最少次数使得矩阵全$0$ 贪心, $n>m$时每次取一列, 否则取一行 #inclu ...
随机推荐
- Beta冲刺合集
Beta冲刺序列: Beta凡事预则立 :Beta No.0 Beta冲刺Day1:Beta No.1 Beta冲刺Day2:Beta No.2 Beta冲刺Day3:Beta No.3 Beta冲刺 ...
- Beta冲刺第七天
一.昨天的困难 没有困难. 二.今天进度 1.林洋洋:MD图片上传,修复权限问题,修复本地存储判空问题,修复协作申请没有过滤问题. 2.黄腾达:添加文件链接和邀请链接复制功能,协作树界面优化. 3.张 ...
- Flask 扩展 自定义扩展
创建一个为视图访问加日志的扩展Flask-Logging,并从中了解到写Flask扩展的规范. 创建工程 先创建一个工程,目录结构如下: flask-logging/ ├ LICENSE # 授权说明 ...
- SourceTree 实现 git flow 流程
为什么使用 git 和 git flow,这篇文章 深入理解学习Git工作流 的内容相信能够给你一个完整的答案. 我们以使用SVN的工作流来使用git有什么不妥? git 方便的branch在哪里,团 ...
- JAVA_SE基础——18.方法的递归
方法的递归是指在一个方法的内部调用自身的过程,递归必须要有结束条件,不然就会陷入无限递归的状态,永远无法结束调用,接下来用一个最简单的例子来体现下方法递归,使用递归算法计算自然数之和: public ...
- python+flask 分分钟完美解析阿里云日志
拿到了自己阿里云服务器的日志,对其需要进行处理. class Read_Rizhi: def __init__(self,filename): self.filename=filename def o ...
- nodeJs多进程Cluster
在前端页面中,如果我们想进行多进程,我们会用到WebWorker,而在NodeJs中,我们如果想充分利用服务器核心资源,我们会用到Node中Cluster模块 直接上代码吧: const cluste ...
- nodeJS基于smtp发邮件
邮件的协议smtp是tcp/ip族中的一个协议,所以我们这次考虑使用net模块来发送邮件. const net = require('net') const assert = require('ass ...
- LeetCode & Q20-Valid Parentheses-Easy
Stack String Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ...
- Linq 延迟加载
IList<Student> ssList = new List<Student>() { , StudentName = "John", } , , St ...