Evanyou Blog 彩带

CF815C Karen and Supermarket

题意翻译

在回家的路上,凯伦决定到超市停下来买一些杂货。 她需要买很多东西,但因为她是学生,所以她的预算仍然很有限。

事实上,她只花了b美元。

超市出售N种商品。第i件商品可以以ci美元的价格购买。当然,每件商品只能买一次。

最近,超市一直在努力促销。凯伦作为一个忠实的客户,收到了n张优惠券。

如果凯伦购买i次商品,她可以用i次优惠券降低di美元。 当然,不买对应的商品,优惠券不能使用。

然而,对于优惠券有一个规则。对于所有i>=2,为了使用i张优惠券,凯伦必须也使用第xi张优惠券 (这可能意味着使用更多优惠券来满足需求。)

凯伦想知道。她能在不超过预算B的情况下购买的最大商品数量是多少?

感谢@luv_letters 提供的翻译

题目描述

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 b b dollars.

The supermarket sells n n n goods. The i i i -th good can be bought for ci c_{i} 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 n n coupons. If Karen purchases the i i i -th good, she can use the i i i -th coupon to decrease its price by di d_{i} 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 i>=2 i>=2 , in order to use the i i i -th coupon, Karen must also use the xi x_{i} 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 b b ?

输入输出格式

输入格式:

The first line of input contains two integers n n n and b b b ( 1<=n<=5000 1<=n<=5000 1<=n<=5000 , 1<=b<=109 1<=b<=10^{9} 1<=b<=109 ), the number of goods in the store and the amount of money Karen has, respectively.

The next n n n lines describe the items. Specifically:

  • The i i i -th line among these starts with two integers, ci c_{i} ci and di d_{i} di ( 1<=di<ci<=109 1<=d_{i}<c_{i}<=10^{9} 1<=di<ci<=109 ), the price of the i i i -th good and the discount when using the coupon for the i i i -th good, respectively.
  • If i>=2 i>=2 i>=2 , this is followed by another integer, xi x_{i} xi ( 1<=xi<i 1<=x_{i}<i 1<=xi<i ), denoting that the xi x_{i} 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.

输入输出样例

输入样例#1: 
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
输出样例#1: 
4
输入样例#2: 
5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
输出样例#2: 
5

说明

In the first test case, Karen can purchase the following 4 4 4 items:

  • Use the first coupon to buy the first item for 10−9=1 10-9=1 109=1 dollar.
  • Use the third coupon to buy the third item for 12−2=10 12-2=10 122=10 dollars.
  • Use the fourth coupon to buy the fourth item for 20−18=2 20-18=2 2018=2 dollars.
  • Buy the sixth item for 2 2 2 dollars.

The total cost of these goods is 15 15 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.

 

Solution:

  好像有蛮久没搞dp了,看到这题既然只会暴力。

  首先题目很显然是一个树形结构,然后必须父节点被标记(打特价),子节点才能标记(打特价),应该很容易想到树形dp的。

  定义状态$f[i][j][k]$表示在$i$节点所在的子树中选了$j$个物品且$i$节点状态为$k,k\in[0,1]$的最少花费,那么转移就显而易见了:设$w[i]$表示$i$的原价,$c[i]$表示$i$的特价,则$f[u][i+j][0]=min(f[u][i+j][0],f[u][i][0]+f[v][j][0]),f[u][i+j][1]=min(f[u][i+j][1],f[u][i][1]+f[v][j][1]),f[u][i+j][1]=min(f[u][i+j][1],f[u][i][1]+f[v][j][0])$,注意初始化$f[u][0][0]=0,f[u][1][0]=w[u],f[u][1][1]=c[u]$,最后只要输出$f[1][i][0]$和$f[1][i][1]$中最大的使得花费$\leq m$的$i$啦。

代码:

 

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=5005;
int n,m,to[N],net[N],h[N],w[N],c[N],cnt,siz[N];
ll f[N][N][2];

il int gi(){
    int a=0;char x=getchar();bool f=0;
    while((x<'0'||x>'9')&&x!='-')x=getchar();
    if(x=='-')x=getchar(),f=1;
    while(x>='0'&&x<='9')a=(a<<3)+(a<<1)+(x^48),x=getchar();
    return f?-a:a;
}

il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;}

il int dfs(int u){
    siz[u]=1;
    f[u][0][0]=0;
    f[u][1][0]=w[u],f[u][1][1]=c[u];
    for(int p=h[u];p;p=net[p]){
        dfs(to[p]);
        Bor(i,0,siz[u]) For(j,0,siz[to[p]]) 
            f[u][i+j][0]=min(f[u][i+j][0],f[to[p]][j][0]+f[u][i][0]),
            f[u][i+j][1]=min(f[u][i+j][1],f[to[p]][j][0]+f[u][i][1]),
            f[u][i+j][1]=min(f[u][i+j][1],f[to[p]][j][1]+f[u][i][1]);
        siz[u]+=siz[to[p]];
    }
}

int main(){
    n=gi(),m=gi();
    w[1]=gi(),c[1]=w[1]-gi();
    memset(f,0x3f,sizeof(f));
    int u;
    For(i,2,n) w[i]=gi(),c[i]=w[i]-gi(),u=gi(),add(u,i);
    dfs(1);
    Bor(i,0,n) if(f[1][i][0]<=m||f[1][i][1]<=m) printf("%d",i),exit(0);
    return 0;
}

 

 

 

posted @ 2018-08-16 13:53  five20  阅读(166)  评论(0编辑  收藏  举报
Live2D