1. 程式人生 > >Codeforces 815 C 樹形依賴揹包 解題報告

Codeforces 815 C 樹形依賴揹包 解題報告

C. 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?

Input

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

Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.

Examples

input
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
output
4
input
5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
output
5

【解題報告】
題意:
商店裡有n個物品,第i個物品的價格為ci元。每個物品只能買一次。商店發行了n張優惠券,每個物品各有一張優惠卷。如果使用了第i張優惠券,可以使該物品便宜di元錢,必須買商品才能夠使用相對應的優惠券。第1張優惠卷可以無條件使用,但對於第i>=2張優惠卷,如果需要使用第i張優惠券,則必須先使用xi這張優惠券。
現在有b元錢,問最多能購買多少商品。

考慮用樹上動態規劃來解決問題。
設f[u][k]表示以u為根的子樹買k個物品的最小花費,且物品u使用了優惠卷;
設g[u][k]表示以u為根的子樹買k個物品的最小花費,且物品u不使用優惠卷;
假設v是u的一個兒子結點,那麼狀態轉移方程為:
f’[u][i + j] = min(f’[u][i + j], f[v][j] + f[u][i]);
f’[u][i + j] = min(f’[u][i + j], g[v][j] + f[u][i]);
g’[u][i + j] = min(g’[u][i + j], g[v][j] + g[u][i]);
理論複雜度O(n3)
樹上動態規劃的獨特優化可以做到O(n2),詳見參考程式碼,著重注意迴圈的界限以及size域的變化。

程式碼如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 5010
#define inf 0x3f3f3f3f

int cnt=-1,head[N];
struct Edge{int to,nxt;}e[N<<1];
int n,b,ans;
int c[N],d[N],w[N];
int dp[N][N][2],size[N];

void adde(int u,int v)
{
    e[++cnt].to=v;
    e[cnt].nxt=head[u];
    head[u]=cnt;
}
void dfs(int u)
{
    size[u]=1;dp[u][0][0]=0;
    dp[u][1][0]=c[u];dp[u][1][1]=c[u]-d[u];
    for(int i=head[u];~i;i=e[i].nxt)
    {
        int v=e[i].to;dfs(v);
        for(int j=size[u];j>=0;j--)
        for(int k=0;k<=size[v];k++)
        {
            dp[u][j+k][0]=min(dp[u][j+k][0],dp[u][j][0]+dp[v][k][0]);
            dp[u][j+k][1]=min(dp[u][j+k][1],dp[u][j][1]+dp[v][k][1]);
            dp[u][j+k][1]=min(dp[u][j+k][1],dp[u][j][1]+dp[v][k][0]);
        }
        size[u]+=size[v];
    }
}

int main()
{
    freopen("shopping.in","r",stdin);
    freopen("shopping.out","w",stdout);
    memset(head,-1,sizeof(head));
    memset(dp,inf,sizeof(dp));
    scanf("%d%d",&n,&b);
    scanf("%d%d",&c[1],&d[1]);
    for(int i=2;i<=n;i++)
    {
        int u;scanf("%d%d%d",&c[i],&d[i],&u);
        adde(u,i);  
    }
    dfs(1);ans=n;
    while(dp[1][ans][0]>b&&dp[1][ans][1]>b) ans--;
    printf("%d",ans);
    return 0;
}