树形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
 
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; struct Edge
{
int to,next;
}e[]; int dp[][],c[],v[],Adj[],Size,n,m,f; void Init()
{
Size=;
memset(Adj,-,sizeof(Adj));
} void Add_Edge(int u,int v)
{
///u--->v
e[Size].to=v;
e[Size].next=Adj[u];
Adj[u]=Size++;
}
/*
void ShowGraph()
{
for(int i=0;i<=n;i++)
{
for(int j=Adj[i];~j;j=e[j].next)
{
int v=e[j].to;
cout<<i<<"--->"<<v<<endl;
}
}
}
*/ void dfs(int p,int G)
{
int son;
for(int i=Adj[p];~i;i=e[i].next)
{
son=e[i].to;
if(~Adj[son])
{
for(int j=;j<=G-c[son];j++)
dp[son][j]=dp[p][j]+v[son];///对子节点进行赋值
dfs(son,G-c[son]);///递归求解(根节点一定取)
for(int j=G;j>=c[son];j--)
dp[p][j]=max(dp[p][j],dp[son][j-c[son]]);///合并的过程
}
else
{
for(int j=G;j>=c[son];j--)
dp[p][j]=max(dp[p][j],dp[p][j-c[son]]+v[son]);///是叶节点,就是普通01背包
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
Init();
c[]=v[]=;
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&c[i],&v[i],&f);
if(i!=f)
Add_Edge(f,i);
else
Add_Edge(,i);
}
///ShowGraph();
dfs(,m);
printf("%d\n",dp[][m]);
}
return ;
}

HDOJ 3593 The most powerful force的更多相关文章

  1. Spring In action chapter1_wiringBeans

    Automatically wiring beans Spring attacks automatic wiring from two angles: Component scanning-Sprin ...

  2. Approaching the Fun Factor in Game Design

    I recently did some research on this and talked to Dr. Clayton Lewis (computer Scientist in Residenc ...

  3. 4.2 面向对象分析(二) CRC方法标识概念类

    CRC  又称为CRC索引卡片:CRC card  每张卡片代表一个类 Each card represents one class  每张卡片上写出这个类承担的职责.与其合作交互的其他类名   ...

  4. MeToo, one year on

    表示转折/让步关系:but, however, nevertheless, whereas, although, despite, in spite of, still 表示比较或对比关系:simil ...

  5. android设计准则

    ------------缘由-------------------------------------------------------------------------------------- ...

  6. Brute force Attack

    1 Introduction A common threat that webdevelopers face is a password-guessing attack known as a brut ...

  7. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. python——复制目录结构小脚本

    引言 有个需要,需要把某个目录下的目录结构进行复制,不要文件,当目录结构很少的时候可以手工去建立,当目录结构复杂,目录层次很深,目录很多的时候,这个时候要是还是手动去建立的话,实在不是一种好的方法,弄 ...

  2. lbs(查看附近的人),看看社交软件如何实现查看附近的人

    最近在做一款移动端棋牌游戏,为了进一步提高用户体验.拉近玩家的距离,我们决定在游戏中加入好友功能,而对于体验好友功能的玩家来说,要是玩牌的时候可以看看附近都有谁在玩牌,跟他们交流交流玩牌心得什么的无疑 ...

  3. CF 208A Dubstep(简单字符串处理)

    题目链接: 传送门 Dubstep Time Limit: 1000MS     Memory Limit: 32768 KB Description Vasya works as a DJ in t ...

  4. 个人作业—Week3

    博客阅读体会 阅读了十几位软件工程师前辈的博文,了解了前辈们作为一名软件工程师的成长经历,我有一些感触. 这十几位前辈们的经历有着很大的差别,有的科班出身,有的则完全自学成才.不同的经历使得前辈们看问 ...

  5. JPA事务总结

    http://www.soso.io/article/65405.html 事务管理是JPA中另一项重要的内容,了解了JPA中的事务管理,能够进一步掌握JPA的使用.事务管理是对一系列操作的管理,它最 ...

  6. dotnetbar入门

    1.下载dotnetbar组件 2.工具箱引用 3.项目引用 4.开始工作 //此处Form完整的名称是System.Windows.Forms.Form,表示FrmMain窗体类是继承于System ...

  7. jquery selector 基础

    转自:http://www.cnblogs.com/zwl12549/archive/2008/08/09/1264163.html query的这套选择符是比较帅气的,借用了XPath2.0和CSS ...

  8. 最大似然估计 (MLE) 最大后验概率(MAP)

    1) 最大似然估计 MLE 给定一堆数据,假如我们知道它是从某一种分布中随机取出来的,可是我们并不知道这个分布具体的参,即"模型已定,参数未知". 例如,我们知道这个分布是正态分布 ...

  9. Java——列表框:JList

    import java.awt.Container; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import j ...

  10. C++ Pointer-to-Member Selector

    http://www.codeguru.com/cpp/cpp/article.php/c17401/C-Tutorial-PointertoMember-Function.htm https://m ...