[原创] 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的一 ...
随机推荐
- iOS学习笔记13-网络(二)NSURLSession
在2013年WWDC上苹果揭开了NSURLSession的面纱,将它作为NSURLConnection的继任者.现在使用最广泛的第三方网络框架:AFNetworking.SDWebImage等等都使用 ...
- SPOJ GSS5 Can you answer these queries V ——线段树
[题目分析] GSS1上增加区间左右端点的限制. 直接分类讨论就好了. [代码] #include <cstdio> #include <cstring> #include & ...
- haskell 乱搞笔记[原创]
脑洞时间:为什么世界上有那么多程序语言,那是腐朽的资本主义为了增加广大人民学习成本以及编译原理太过普及造成的,建议大学取消编译原理的一切课程,并挥起奥姆休的剃刀,把所有程序语言统统踢了,除机器 ...
- 刷题总结——宠物收养所(bzoj1208)
题目: Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明 ...
- Hibernate批量更新和批量删除批量添加(转)
通常,在一个Session对象的缓存中只存放数量有限的持久化对象,等到Session对象处理事务完毕,还要关闭Session对象,从而及时释放Session的缓存占用的内存.批量处理数据是指在一个事务 ...
- GFS, HDFS, Blob File System架构对比
分布式文件系统很多,包括GFS,HDFS,淘宝开源的TFS,Tencent用于相册存储的TFS (Tencent FS,为了便于区别,后续称为QFS),以及Facebook Haystack.其中,T ...
- [C/C++] 结构体内存对齐用法
一.为什么要内存对齐 经过内存对齐之后,CPU的内存访问速度大大提升; 内存空间按照byte划分,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内 ...
- Python入门--2--继续学习
继续学习小甲鱼 一.python比较操作符 == :判断左边是否等于右边 != : 判断左边是否不能右边 二. if while判断语句 栗子: temp = input ("sha shu ...
- centos7 搭建hadoop
参考文档:http://blog.csdn.net/xiaoxiangzi222/article/details/52757168 https://waylau.com/centos-7-instal ...
- android 禁止ViewPager滑动
最近项目中,有个需求就是要禁止ViewPager滑动事件,我们看下360手机助手的界面,风格就类似这样的 大家如果使用过360手机助手就会发现中间内容是不可以滑动的,现在写一个demo,讲下怎么禁止V ...