HDOJ 3593 The most powerful force

树形DP / 泛化物品的背包。。。可以去看09年徐持衡论文《浅谈几类背包问题》

The most powerful force

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 727    Accepted Submission(s): 182


Problem Description
The leaders of AC country made a decision to regain the TW Island. But they have to face the powerful force from USB. So the leaders have to hire some excellent soldiers from the training base called ALPC to format the most powerful force.
The followings are the consists of ALPC:
1.  There are more than one alpcs who are generals, like alpc04.
2.  Every alpc has a direct superior, except the generals.
3.  The alpc who has underling is an officer (Every officer’ number is no more than 500, like alpc21, alpc32).
4.  If send one alpc to the war, so will his direct superior. Of course the general’s direct superior is himself.
5.  Every alpc has two values. Vi is the fighting strength and Ci is the cost that the leader have to pay for the soldier.

Due to the AC country has limited financial resources, the leaders want to format the most powerful force with the cost not exceed G. Obviously, it is an uphill task! Therefore, the leaders of the AC country have assigned the task to alpc72 because of the outstanding behavior he had done at the ACM/ICPC. But alpc72 has been lost in hunting PLMM nowadays, so that he has no time to deal with this problem. What’s more, he left it to you.
 

 

Input
There will have several test cases. 
The first line has two integers N and G, which stand for the total number of soldiers and the maximum cost. Soldiers numbered consequently from 1 to N. (1<=N<=100000, 1<=G<=10000)
The next N lines, each has three integers Ci, Vi and Fi. They are the cost, the measure of power and the direct superior of the ith soldier. (0<=Ci<=1000000, 0<=Vi<=1000000)
 

 

Output
Output one integer (must within 32 bits)as the maximum fighting strength of the most powerful force.
 

 

Sample Input
5 10
1 2 1
10 5 2
1 1 1
1 1 1
1 1 3
5 10
1 2 1
2 4 2
1 1 1
1 1 1
1 1 3
 

 

Sample Output
5
9
Hint
Special Thanks to alpc30.
 

 

Author
alpc32
 

 

Source
 

 

Recommend
zhengfeng
 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 struct Edge
 8 {
 9     int to,next;
10 }e[110000];
11 
12 int dp[600][11000],c[110000],v[110000],Adj[110000],Size,n,m,f;
13 
14 void Init()
15 {
16     Size=0;
17     memset(Adj,-1,sizeof(Adj));
18 }
19 
20 void Add_Edge(int u,int v)
21 {
22     ///u--->v
23     e[Size].to=v;
24     e[Size].next=Adj[u];
25     Adj[u]=Size++;
26 }
27 /*
28 void ShowGraph()
29 {
30     for(int i=0;i<=n;i++)
31     {
32         for(int j=Adj[i];~j;j=e[j].next)
33         {
34             int v=e[j].to;
35             cout<<i<<"--->"<<v<<endl;
36         }
37     }
38 }
39 */
40 
41 void dfs(int p,int G)
42 {
43     int son;
44     for(int i=Adj[p];~i;i=e[i].next)
45     {
46         son=e[i].to;
47         if(~Adj[son])
48         {
49             for(int j=0;j<=G-c[son];j++)
50                 dp[son][j]=dp[p][j]+v[son];///对子节点进行赋值
51             dfs(son,G-c[son]);///递归求解(根节点一定取)
52             for(int j=G;j>=c[son];j--)
53                 dp[p][j]=max(dp[p][j],dp[son][j-c[son]]);///合并的过程
54         }
55         else
56         {
57             for(int j=G;j>=c[son];j--)
58                 dp[p][j]=max(dp[p][j],dp[p][j-c[son]]+v[son]);///是叶节点,就是普通01背包
59         }
60     }
61 }
62 
63 int main()
64 {
65     while(scanf("%d%d",&n,&m)!=EOF)
66     {
67         Init();
68         c[0]=v[0]=0;
69         memset(dp,0,sizeof(dp));
70         for(int i=1;i<=n;i++)
71         {
72             scanf("%d%d%d",&c[i],&v[i],&f);
73             if(i!=f)
74                 Add_Edge(f,i);
75             else
76                 Add_Edge(0,i);
77         }
78         ///ShowGraph();
79         dfs(0,m);
80         printf("%d\n",dp[0][m]);
81     }
82     return 0;
83 }

 

 

 

转载于:https://www.cnblogs.com/CKboss/p/3360428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值