HDU 3593 The most powerful force

来源:互联网 发布:农村淘宝合伙人挣钱吗 编辑:程序博客网 时间:2024/06/15 06:57

ProblemDescription

The leaders of ACcountry made a decision to regain the TW Island. But they have to face thepowerful force from USB. So the leaders have to hire some excellent soldiersfrom 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’ numberis no more than 500, like alpc21, alpc32).
4.  If send one alpc to the war, so will his direct superior. Ofcourse the general’s direct superior is himself.
5.  Every alpc has two values. Vi is the fighting strength and Ci isthe cost that the leader have to pay for the soldier.

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

 

 

Input

There will haveseveral test cases. 
The first line has two integers N and G, which stand for the total number ofsoldiers 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, themeasure 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 powerfulforce.

 

 

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

题目大意:有N个点,给定G元钱。每个点CiViFi分别表示需要的价钱,战斗力,和直接上司。如果第i行的Fi==i表示,自己是最高首领。所以,对于这道题来说,输入的数据是一个森林。我们可以将每棵树的根节点连接到一个点上面。因为编号是从1开始的,所以所有根节点都连接到0上面,这样这道题就变成了一棵树了。对于0来说,CiVi皆为0.现在要在G元钱里选取最强的战斗力,并且选择一个点,那么就必须选择其父亲节点。故,题目可以看做一个背包问题来解决。对于叶子节点,其本身是一件物品,而对于非叶子节点,可以看做是将其子节点全部合并后得到的一个叶子节点,对于其来说给定不同的钱可以获得不同的价值,即可以看做是一个泛化背包。

题目中有一个关键的信息是Every officernumber is no more than 500。即是,非叶子节点的编号不超过500.dp[510][10010]就可以储存。

因为选取当前节点,必须选择其父亲节点,那么到当前节点能用的钱,即是G-0到当前节点父亲节点的花费。

dp[i][j]的意义是,以i为根节点的子树(不包括i点)花费j元能获得的最大战斗力。

转移方程:dp[x][j] = max(dp[x][j],dp[re][j-c[re]] + u[re]);

 

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<vector>#include<algorithm>using namespace std;int n ,g ,dp[510][10010] ,m;int c[100010] ,u[100010];vector<int> v[100010];void solve(int x ,int spend){    int re;    for(int i = 0;i < v[x].size();i++)    {        re = v[x][i];        //对于子节点为叶子节点的点,直接进行dp操作合并。        if(v[re].empty())        {            for(int j = spend;j>=c[re];j--)            {                dp[x][j] = max(dp[x][j] ,dp[x][j-c[re]] + u[re]);                if(dp[x][j] > m)                {                    m = dp[x][j];                }            }        }        else        {            for(int j = 0;j<=spend - c[re];j++)            {                dp[re][j] = dp[x][j];            }            //对dp[re][0~spend-c[re]]更行,算出其子树的一个泛化背包。即花费j=1~spend-c[re]能获得的最大战斗力            solve(re,spend-c[re]);            for(int j = spend;j>=c[re];j--)            {                dp[x][j] = max(dp[x][j],dp[re][j-c[re]] + u[re]);                if(dp[x][j] > m)                {                    m = dp[x][j];                }            }        }    }}int main(){    int pre;    while(~scanf("%d%d",&n,&g))    {        m = 0;        memset(dp,0,sizeof(dp));        for(int i = 1;i <= n;i++)        {            scanf("%d%d%d",&c[i],&u[i],&pre);            if(pre==i)            {                v[0].push_back(i);            }            else            {                v[pre].push_back(i);            }        }        solve(0,g);        printf("%d\n",m);        for(int i = 0;i<=n;i++)        {            v[i].clear();        }    }    return 0;}


 

0 0
原创粉丝点击