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

而在本题中j


#include 
using namespace std;
typedef long long ll;
const int N=2005;
vector 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;i0;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<



你可能感兴趣的:(hihocoder,dp-----------,动态规划,树dp)