There N N article and a capacity of a V backpack of V.
Having a dependency between items, and the composition dependence of the shape of a tree. If you select an item, you must select its parent.
As shown below:
If the selected item 5, you must select the items 1 and 2. This is because 2 5 is a parent node, the parent node is 2 1.
Each item number is I I, the volume is V I VI, is the value W I Wi, the parent node ID is dependent P I PI. Subscript article is range . 1 ... N . 1 ... N.
Which solving the items into the bag, the total volume of the article can not exceed the capacity of the backpack, and the total value of the maximum.
The maximum output value.
Input Format
The first line has two integers N , V N, V, separated by spaces, respectively, represents the number of items and the capacity of the backpack.
Then there are N N rows, each row represents a data item.
The first I I line has three integers V I , W I , P I VI, Wi, PI, separated by a space, the volume of the article respectively, and the value of item number dependent.
If P I = - . 1 PI = -1, represents the root node. Ensure that all data items constitute a tree.
Output Format
Output An integer representing the maximum value.
data range
1≤N,V≤1001≤N,V≤100
1≤vi,wi≤1001≤vi,wi≤100
Parent Id range:
- Internal node: . 1 ≤ P I ≤ N 1≤pi≤N;
- Root P I = - . 1 PI = -1;
SAMPLE INPUT
5 7
2 3 -1
2 2 1
3 5 1
4 7 2
3 6 2
Sample output:
11
There backpack nine stresses dependent knapsack problem
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 105; int N, V, p, root, ind=0; int v[maxn], w[maxn]; int ch[maxn], nxt[maxn], pre[maxn]; int dp[maxn][maxn]; void add(int p,int i){ ch[ind] = i; nxt[ind] = pre[p]; pre[p] = ind++; } void dfs(int u){ for(int i=pre[u];i!=-1;i=nxt[i]){ int son = ch[i]; dfs(son); for(int j=V-v[u];j>=v[son];j--) for(int k=v[son];k<=j;k++) dp[u][j] = max(dp[u][j],dp[u][j-k]+dp[son][k]); } for(int i=V;i>=v[u];i--) dp[u][i] = dp[u][i-v[u]]+w[u]; //for(int i=0;i<v[u];i++) // dp[u][i] = 0; } int main(){ memset(pre,-1,sizeof(pre)); scanf("%d%d",&N,&V); for(int i=1;i<=N;i++){ scanf("%d%d%d",&v[i],&w[i],&p); if(p==-1) root = i; else add(p,i); } dfs(root); printf("%d\n",dp[root][V]); return 0; }