hihocoder #1500 : EL SUENO 樹DP

#1500 : EL SUENO

時間限制:10000ms
單點時限:1000ms
內存限制:256MB

描述

In a video game, Little Hi is going to assassinate the leader of an evil group, EL SUENO.

There are N high-value targets in the group, numbered from 1 to N. Each target has another target as his direct superior except for EL SUENO who has no superior. So the superior-subordinate hierarchy forms a tree-like structure. EL SUENO is at the root.

The cost of assassinating the i-th target is Ci. Before assassinating a target Little Hi has to obtain enough information about him. For the i-th target, Little Hi needs INi units of information. By assassinating a target Little Hi will obtain some information about the target's direct superior. More specifically, the i-th target has IPi units of information about his superior. So in order to assassinate EL SUENO, Little Hi needs to assassinate some of his direct subordinates so that the sum of information obtained is enough; assassinating the subordinates needs to assassinate their direct subordinates ... until it reaches some targets require zero information in advance. (Luckily if a target has no subordinate he always requires zero information.)

How Little Hi wants to know what is the minimum cost for successful assassinating EL SUENO.  

輸入

The first line contains an integer N.

Then follow N lines. Each line describes a target with 4 integers, Fi, INi, IPi, Ci.  

Fi is i-th target's superior. For EL SUENO his Fi is zero.

INi is the amount of information needed to assassinate the i-th target. For a target has no subordinates his INi is always zero.

IPi is the amount of information the i-th target has about his superior Fi.

Ci is the cost of assassinating the i-th target.

For 30% of the data, 1 <= N <= 10, 0 <= INi, IPi <= 100

For 60% of the data, 1 <= N <= 100, 0 <= INi, IPi <= 1000

For 100% of the data, 1 <= N <= 2000, 0 <= Fi <= N, 0 <= INi, IPi <= 20000, 1 <= Ci <= 1000.

It is guaranteed that the N targets form a tree structure.

輸出

The minimum cost. If Little Hi is not able to assassinate EL SUENO output a number -1.

樣例輸入
6
2 1 2 2
0 4 0 2
2 0 2 5
2 0 2 6
1 0 1 2
1 0 1 3
樣例輸出
11
.

.

題意:

輸入 F,IN,IP,C數組

n個人構成一個樹的關係,要殺死第i個人 必須要in[i]的儲蓄值,而這個值只能從殺死他的後代來得到,每殺死一個後代,能對其直接祖先貢獻IP[i]的儲蓄值

恩 還有殺死一個人要額外代價C[i]

最後問你殺死根節點的最小代價是多少


每一層直接做01揹包

注意 這個和裸的01揹包有點微小的不同是,我們枚舉容量j的時候當遇到J<volume[i] 時,也就是放不下了時,我們就可以停止循環了

而在本題中j<ip[i]時,也就是這個人的貢獻值超過了所需要的儲蓄值時,我們還需要更新答案,這種情況是合法的,僅此不同。


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2005;
vector<int > mp[N];
ll n;
ll f[N];
ll in[N];
ll ip[N];
ll c[N];
ll dfs(int x)
{
    ll dp[20020];
    dp[0]=0;
    for(int i=1;i<=in[x];i++)dp[i]=1e16;
    for(int i=0;i<mp[x].size();i++)
    {
        int v=mp[x][i];
        ll tmp=dfs(v);
        for(int j=in[x];j>0;j--)
            dp[j]=min(dp[j],dp[max(j-ip[v],0LL)]+c[v]+tmp);
    }
    return dp[in[x]];
}
int main()
{

    int n;
    cin>>n;
    int root;
    for(int i=1;i<=n;i++)
    {
        cin>>f[i]>>in[i]>>ip[i]>>c[i];
        if (!f[i])root=i;
        mp[f[i]].push_back(i);
    }
    ll ans=dfs(root);
    if (ans>=1e16) cout<<-1<<endl;
    else
    cout<<ans+c[root]<<endl;
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章