树形DP(3) Hdu3593 The most powerful force (泛化背包)

2024-05-10 16:32

本文主要是介绍树形DP(3) Hdu3593 The most powerful force (泛化背包),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3593

The most powerful force

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


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.


题意:为了得到最强大的战力,现有G的金钱,N个士兵,每个士兵有一个 cost值 和 战斗力值,询问最多能得到多大的战力。

限制条件:士兵之间有上下属关系,每个士兵只有一个直接上司(或自己),可以没有下属,同时,若取了某个士兵,则他的上司也必须参加。

分析:1>.整个军队关系图 是一个森林,因此加一个0号结点做 每颗子树的父亲,则森林变成了树。

2> 对于下属和上司必须同时选取的 限制条件,限制了取的情况:只取上司,取上司和一个下属,取上司和一个下属和下属的下属。。。而这些情况都是互斥的,即在根节点的子树里,只能有一种选取方案,而分配给子树的金钱不同,就可以得到不同的战力值,于是 就可以将每个非叶子节点看做泛化背包(可参见背包九讲-6,7,8讲),将叶子结点当做01背包来做。由根节点 沿树递归搜到叶子节点,做完叶子节点的更新,再由叶子更新父亲结点,就可以更新到0号结点。具体可以看代码注释。

代码:

#include<cstdio>
#include<algorithm>
#define MAX 100005
using namespace std;
bool leaf[MAX];
int dp[505][10005];//非叶子节点最多500个
int G;
int cost[MAX],val[MAX];
struct node{int to,nxt;
}edg[MAX];
int head[MAX];
int ind=0;
void dfs(int now,int lim)
{int v;for (int i = head[now]; i != -1; i = edg[i].nxt){v = edg[i].to;if(leaf[v])	{//叶子节点for (int j = lim; j >= cost[v]; j--){//对叶子结点做01背包,更新父节点dp[now][j] = max(dp[now][j-cost[v]]+val[v],dp[now][j]);}}else{//非叶子节点if(lim >= cost[v]){for (int j = lim-cost[v]; j >=0 ; j--){//强制将子节点v放入父节点的泛化背包中,因为若想取子节点v的子节点,题目要求要先取v才行,这样才能更新子节点的值。dp[v][j] = dp[now][j]+val[v];}dfs(v,lim-cost[v]);//对儿子节点做背包。for (int k = cost[v]; k <= lim; k++)//合并,更新父亲结点dp[now][k] = max(dp[v][k-cost[v]],dp[now][k]);}}}
}
void addedge(int u,int v)
{edg[ind].to = v;edg[ind].nxt = head[u];head[u] = ind++;
}
int main()
{int n;while(~scanf("%d %d",&n,&G)){ind=0;memset(dp[0],0,sizeof(dp));//把零号节点初始化为0。memset(head,-1,sizeof(head));memset(leaf,1,sizeof(leaf));cost[0] = 0;int fi;for (int i = 1; i <= n; i++){scanf("%d %d %d",&cost[i],&val[i],&fi);if(fi == i)		fi = 0;			//加上0号节点,原图就由森林变成了树,便于处理。addedge(fi,i);leaf[fi] = 0;}	dfs(0,G);	//从根节点开始递归。	printf("%d\n",dp[0][G]);}return 0;
}




这篇关于树形DP(3) Hdu3593 The most powerful force (泛化背包)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/976969

相关文章

poj2576(二维背包)

题意:n个人分成两组,两组人数只差小于1 , 并且体重只差最小 对于人数要求恰好装满,对于体重要求尽量多,一开始没做出来,看了下解题,按照自己的感觉写,然后a了 状态转移方程:dp[i][j] = max(dp[i][j],dp[i-1][j-c[k]]+c[k]);其中i表示人数,j表示背包容量,k表示输入的体重的 代码如下: #include<iostream>#include<

hdu2159(二维背包)

这是我的第一道二维背包题,没想到自己一下子就A了,但是代码写的比较乱,下面的代码是我有重新修改的 状态转移:dp[i][j] = max(dp[i][j], dp[i-1][j-c[z]]+v[z]); 其中dp[i][j]表示,打了i个怪物,消耗j的耐力值,所得到的最大经验值 代码如下: #include<iostream>#include<algorithm>#include<

csu(背包的变形题)

题目链接 这是一道背包的变形题目。好题呀 题意:给n个怪物,m个人,每个人的魔法消耗和魔法伤害不同,求打死所有怪物所需的魔法 #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>//#include<u>#include<map

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

hdu4865(概率DP)

题意:已知前一天和今天的天气概率,某天的天气概率和叶子的潮湿程度的概率,n天叶子的湿度,求n天最有可能的天气情况。 思路:概率DP,dp[i][j]表示第i天天气为j的概率,状态转移如下:dp[i][j] = max(dp[i][j, dp[i-1][k]*table2[k][j]*table1[j][col] )  代码如下: #include <stdio.h>#include

usaco 1.1 Broken Necklace(DP)

直接上代码 接触的第一道dp ps.大概的思路就是 先从左往右用一个数组在每个点记下蓝或黑的个数 再从右到左算一遍 最后取出最大的即可 核心语句在于: 如果 str[i] = 'r'  ,   rl[i]=rl[i-1]+1, bl[i]=0 如果 str[i] = 'b' ,  bl[i]=bl[i-1]+1, rl[i]=0 如果 str[i] = 'w',  bl[i]=b

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

uva 10154 DP 叠乌龟

题意: 给你几只乌龟,每只乌龟有自身的重量和力量。 每只乌龟的力量可以承受自身体重和在其上的几只乌龟的体重和内。 问最多能叠放几只乌龟。 解析: 先将乌龟按力量从小到大排列。 然后dp的时候从前往后叠,状态转移方程: dp[i][j] = dp[i - 1][j];if (dp[i - 1][j - 1] != inf && dp[i - 1][j - 1] <= t[i]