[原创] Karen and Supermarket 2
在回家的路上,凯伦决定到超市停下来买一些杂货。 她需要买很多东西,但因为她是学生,所以她的预算仍然很有限。
事实上,她只花了b美元。
超市出售N种商品。第i件商品可以以ci美元的价格购买。当然,每件商品只能买一次。
最近,超市一直在努力促销。凯伦作为一个忠实的客户,收到了n张优惠券。
如果凯伦购买i次商品,她可以用i次优惠券降低di美元。 当然,不买对应的商品,优惠券不能使用。
然而,对于优惠券有一个规则。对于所有i>=2,为了使用i张优惠券,凯伦必须买第j个商品。
凯伦想知道。她能在不超过预算B的情况下购买的最大商品数量是多少?
输入输出样例
输入样例#1: 复制
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
输出样例#1: 复制
4
Solution
考试题目写挂,看错题了。想看原题的戳这里。树型dp,我们定义\(f[i][j][2]\)代表i结点选了j个节点,当前节点选不选。容易想到dp方程为
\]
\]
\]
\]
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int to,next;
}a[1000100];
int w[101000],v[100100],len,last[101010],son[100010],tot;
int f[5100][5100][2];
void add(int a1,int a2)
{
a[++len].to=a2;
a[len].next=last[a1];
last[a1]=len;
}
void dp(int x,int father)
{
son[x]=1;
for(int i=last[x];i;i=a[i].next)
{
int to=a[i].to;
if(to==father)
continue;
dp(to,x);
for(int k=son[x];k>=0;k--)
for(int j=son[to];j>=0;j--)
{
f[x][k+j][0]=min(f[x][k+j][0],f[to][j][0]+f[x][k][0]);
f[x][k+j][0]=min(f[x][k+j][0],f[to][j][1]+f[x][k][0]);
f[x][k+j][1]=min(f[x][k+j][1],f[to][j][1]-v[to]+f[x][k][1]);
f[x][k+j][1]=min(f[x][k+j][1],f[to][j][0]+f[x][k][1]);
}
son[x]+=son[to];
}
}
int main()
{
//freopen("shopping.in","r",stdin);
//freopen("shopping.out","w",stdout);
memset(f,0x3f,sizeof(f));
int n,s,x;
cin>>n>>s;
cin>>w[1]>>v[1];
w[1]-=v[1];
f[1][0][0]=0;f[1][1][1]=w[1];
for(int i=2;i<=n;i++)
{
scanf("%d%d%d",&w[i],&v[i],&x);
add(x,i);add(i,x);
}
for(int i=2;i<=n;i++)
f[i][0][0]=0,f[i][1][1]=w[i];
dp(1,0);
for(int i=n;i>=0;i--)
{
if(f[1][i][0]<=s||f[1][i][1]<=s)
{cout<<i;return 0;}
}
}
博主蒟蒻,可以随意转载,但必须附上原文链接k-z-j。
[原创] Karen and Supermarket 2的更多相关文章
- Codeforces 815C Karen and Supermarket 树形dp
Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...
- CF815C Karen and Supermarket
题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...
- CF815C Karen and Supermarket [树形DP]
题目传送门 Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some gr ...
- E. Karen and Supermarket
E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- 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 815C Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...
- 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 ...
- Codeforces 815 C Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...
- 【Codeforces 815C】Karen and Supermarket
Codeforces 815 C 考虑树型dp. \(dp[i][0/1][k]\)表示现在在第i个节点, 父亲节点有没有选用优惠, 这个子树中买k个节点所需要花的最小代价. 然后转移的时候枚举i的一 ...
随机推荐
- LogMiner配置使用手册
LogMiner配置使用手册 1 Logminer简介 1.1 LogMiner介绍 Oracle LogMiner 是Oracle公司从产品8i以后提供的一个实际非常有用的分析工具,使用该工具可以轻 ...
- [luoguP1415] 拆分数列(DP)
传送门 t(i,j)表示下标从i到j的数 d[i]表示以i结尾的最小的数的下标 d[i]=max(j) (1<=j<=i && t(d[j-1],j-1)<t(j,i ...
- GTY's birthday gift【矩阵快速幂】
题目大意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次,每次可以向可重集中加入一个数 a+b ...
- 使用反射获取类中的属性(可用于动态返回PO类的列,当做表格的表头)
//利用反射取类中的属性字段 try { Class clazz = Class.forName("houji.bean.model.TaskModel"); Field[] fi ...
- ⑨要写信(codevs 1697)
题目描述 Description 琪露诺(冰之妖精)有操控冷气的能力.能瞬间冻结小东西,比普通的妖精更危险.一直在释放冷气的她周围总是非常寒冷. 由于以下三点原因…… 琪露诺的符卡 冰符“Icicle ...
- linux 内核源码arch/ 目录的前世今生
历史的痕迹:在最新的linux-2.6.31/arch/arm/文件夹下,仍然保留Linux最初向ARM处理器移植的痕迹,最初的移植由黑客完成,在老的移植的代码文件的头部保留着黑客的名字:最初的ARM ...
- es6 递归 tree
function loop(data) { let office = data.map(item => { if(item.type == '1' ||item.type == '2') { i ...
- for 循环进化史
ECMAScript 6已经逐渐普及,经过二十多年的改进,很多功能也有了更成熟的语句,比如 for 循环 这篇博客将介绍一下从最初的 for 循环,到 ES6 的 for-of 等四种遍历方法 先定义 ...
- 配置maven从私服上下载构件
maven 本地setting.xml配置 <!--配置maven从私服上下载构件 --> <profiles> <profile> <repositorie ...
- wc递归统计代码行数
find /path -name '*.cpp' |xargs wc -l