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
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.
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)
Special Thanks to alpc30.
#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的更多相关文章
- Spring In action chapter1_wiringBeans
Automatically wiring beans Spring attacks automatic wiring from two angles: Component scanning-Sprin ...
- Approaching the Fun Factor in Game Design
I recently did some research on this and talked to Dr. Clayton Lewis (computer Scientist in Residenc ...
- 4.2 面向对象分析(二) CRC方法标识概念类
CRC 又称为CRC索引卡片:CRC card 每张卡片代表一个类 Each card represents one class 每张卡片上写出这个类承担的职责.与其合作交互的其他类名 ...
- MeToo, one year on
表示转折/让步关系:but, however, nevertheless, whereas, although, despite, in spite of, still 表示比较或对比关系:simil ...
- android设计准则
------------缘由-------------------------------------------------------------------------------------- ...
- Brute force Attack
1 Introduction A common threat that webdevelopers face is a password-guessing attack known as a brut ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- Appium运行时,error: Logcat capture failed: spawn ENOENT的解决办法
目前发现有以下两种可能: 一:查看环境变量是否配置成功. ANDROIDSDK D:\my_2_softwares\JAVA\adt-bundle-windows-x86-20140702\sdkPA ...
- Docker入门教程(八)Docker Remote API
Docker入门教程(八)Docker Remote API [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第八篇,重点介绍了Docker Remote ...
- MongoDb学习1
目标框架必须是 4.5以上,最新MongoDb.Driver 是 2.2.4(与MongoDb.Driver 的1.x版本差别较大) 官方文档 http://mongodb.github.io/mon ...
- Distinct
SELECT 指令让我们能够读取表格中一个或数个栏位的所有资料.这将把所有的资料都抓出,无论资料值有无重复.在资料处理中,我们会经常碰到需要找出表格内的不同资料值的情况.换句话说,我们需要知道这个表格 ...
- Java递归算法——阶乘
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...
- Java导包——import语句
使用语句import org.common.demo01.Demo: 或者import org.common.demo01.*: 如果一个类声明为public class,则文件名称必须与类名称一致, ...
- c++模板类
c++模板类 理解编译器的编译模板过程 如何组织编写模板程序 前言常遇到询问使用模板到底是否容易的问题,我的回答是:“模板的使用是容易的,但组织编写却不容易”.看看我们几乎每天都能遇到的模板类吧,如S ...
- 取出return array() 数组内容
d.php文件 return array( "0" => 内容一, "1" => 内容二, "2" => 内容三, &qu ...
- 在64位系统上不能安装Matlab notebook的解决方案
在64位系统上不能安装Matlab notebook的解决方案 过程分解 第一步:安装好matlab(附:Matalab R2015a界面) 第二步:在matlab中执行命令 >>note ...
- Redis总结(二)C#中如何使用redis
上一篇讲述了安装redis<Redis总结(一)Redis安装>,同时也大致介绍了redis的优势和应用场景.本篇着重讲解.NET中如何使用redis和C#. Redis官网提供了很多开源 ...