[原创] 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的一 ...
随机推荐
- 【Luogu】P3800点收集(DP)
题目链接 原题解 代码 #include<iostream> #include<cstdio> #include<cstring> #include<ccty ...
- BZOJ [HNOI2015]亚瑟王 ——期望DP
发现每张卡牌最后起到作用只和是否打出去了有关. 而且每张牌打出去的概率和之前的牌打出去的情况有关. 所以我们按照牌的顺序进行DP. 然后记录$i$张牌中打出$j$张的概率,然后顺便统计答案. 直接对系 ...
- BZOJ 3747: [POI2015]Kinoman 【线段树】
Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...
- 【2018.10.27】CXM笔记
一个数大约有 $O(\sqrt(n)/log^2(n))$ 个约数. 1. 一个棋盘,每个格子最开始都是白的.可以按一个格子,它马跳(日字跳)能到达的 $8$ 个格子反色(当前格不反色).问有多少种方 ...
- 【2018.9.20】JOI 2017 Final T3「JOIOI 王国 / The Kingdom of JOIOI」
题目链接 题目描述 JOIOI 王国是一个 $H$ 行 $W$ 列的长方形网格,每个 $1\times 1$ 的子网格都是一个正方形的小区块.为了提高管理效率,我们决定把整个国家划分成两个省 $JOI ...
- 【Vim命令大全】史上最全的Vim命令
曾经使用了两年多的Vim,手册也翻过一遍.虽然现在不怎么用vim了,曾经的笔记还是贴出来,与喜欢vim的朋友分享. 1. 关于Vim vim是我最喜欢的编辑器,也是linux下第二强大的编辑器. 虽然 ...
- PatentTips - GPU Support for Blending
Graphics processing units (GPUs) are specialized hardware units used to render 2-dimensional (2-D) a ...
- Python练习题–持续更新
1.你是一个高级测试工程师,现在要做性能测试,需要你写一个函数,批量生成一些注册使用的账号. 产生的账号是以@163.com结尾,长度由用户输入,产生多少条也由用户输入,用户名不能重复,用户名必须由大 ...
- Codeforces 959 D Mahmoud and Ehab and another array construction task
Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of ...
- 使用webstorm+webpack构建简单入门级“HelloWorld”的应用&&构建使用jquery来实现
使用webstorm+webpack构建简单入门级“HelloWorld”的应用&&构建使用jquery来实现 1.首先你自己把webstorm安装完成. 请参考这篇文章进行安装和破解 ...