hdu 4044 2011北京赛区网络赛E 树形dp ****
专题训练
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Node
{
int to;
int next;
}edge[MAXN*];
int tol;
int head[MAXN];
int dp[MAXN][];
int price[MAXN][];
int power[MAXN][]; void init()
{
memset(head,-,sizeof(head));
tol=;
}
void add(int a,int b)
{
edge[tol].to=b;
edge[tol].next=head[a];
head[a]=tol++;
edge[tol].to=a;
edge[tol].next=head[b];
head[b]=tol++;
}
int n,m;
int tmp[MAXN];
void dfs(int u,int pre)
{
if(head[u]==-||(edge[head[u]].to==pre&&edge[head[u]].next==-))//叶子结点
{//叶子结点的条件不要错了!!!!
for(int i=;i<=m;i++)dp[u][i]=; for(int i=;i<=m;i++)tmp[i]=dp[u][i];
//因为存在价格为0的点。所以倒序的DP并不能保证只取一件物品
for(int i=m;i>=;i--)
{
for(int j=;j<=price[u][];j++)
if(price[u][j]<=i)
dp[u][i]=max(dp[u][i],tmp[i-price[u][j]]+power[u][j]); tmp[i]=dp[u][i];//tmp数组是记录的dp的上一个状态
}
return;
}
for(int i=;i<=m;i++) dp[u][i]=INF;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(v==pre)continue;
dfs(v,u);
for(int j=m;j>=;j--)
{
int t=;
for(int k=;k<=j;k++)//这里k一定要从0开始。
t=max(t,min(dp[u][j-k],dp[v][k]));
dp[u][j]=t;
}
} for(int i=;i<=m;i++)tmp[i]=dp[u][i];
for(int i=m;i>=;i--)
{
for(int j=;j<=price[u][];j++)
if(price[u][j]<=i)
dp[u][i]=max(dp[u][i],tmp[i-price[u][j]]+power[u][j]);
//和上面一样分组背包加了个tmp数组
tmp[i]=dp[u][i];
}
}
int main()
{
int u,v;
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
}
scanf("%d",&m);
for(int i=;i<=n;i++)
{
scanf("%d",&price[i][]);
power[i][]=price[i][];
for(int j=;j<=price[i][];j++)
{
scanf("%d%d",&price[i][j],&power[i][j]);
}
}
dfs(,);
printf("%d\n",dp[][m]);
}
return ;
}
hdu 4044 2011北京赛区网络赛E 树形dp ****的更多相关文章
- hdu 4050 2011北京赛区网络赛K 概率dp ***
题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...
- hdu 4049 2011北京赛区网络赛J 状压dp ***
cl少用在for循环里 #include<cstdio> #include<iostream> #include<algorithm> #include<cs ...
- hdu 4045 2011北京赛区网络赛F 组合数+斯特林数 ***
插板法基础知识 斯特林数见百科 #include<iostream> #include<cmath> #include<cstdio> #include<cs ...
- hdu 4043 2011北京赛区网络赛D 概率+大数 **
推出公式为:P = A(2n,n)/(2^(2n)*n!) 但是不会大数,学完java再补
- hdu 4041 2011北京赛区网络赛B 搜索 ***
直接在字符串上搜索,注意逗号的处理 #include<cstdio> #include<iostream> #include<algorithm> #include ...
- hdu 4046 2011北京赛区网络赛G 线段树 ***
还带这么做的,卧槽,15分钟就被A了的题,居然没搞出来 若某位是1,则前两个为wb,这位就是w #include<cstdio> #include<cstring> #defi ...
- hdu 4035 2011成都赛区网络赛E 概率dp ****
太吊了,反正我不会 /* HDU 4035 dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点 ...
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- hdu 4031 2011成都赛区网络赛A题 线段树 ***
就是不知道时间该怎么处理,想了好久,看了别人的题解发现原来是暴力,暴力也很巧妙啊,想不出来的那种 -_-! #include<cstdio> #include<iostream&g ...
随机推荐
- 深入理解学习Git工作流
http://blog.csdn.net/hongchangfirst/article/list/3 //可以看看 http://blog.csdn.net/hongchangfirst/articl ...
- jQuery之元素筛选
1.eq() 筛选指定索引号的元素2.first() 筛选出第一个匹配的元素3.last() 筛选出最后一个匹配的元素4.hasClass() 检查匹配的元素是否含有指定的类5.filter() ...
- Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree
Verify Preorder Sequence in Binary Search Tree \Given an array of numbers, verify whether it is the ...
- (转)高性能网站架构之缓存篇—Redis集群搭建
看过 高性能网站架构之缓存篇--Redis安装配置和高性能网站架构之缓存篇--Redis使用配置端口转发 这两篇文章的,相信你已经对redis有一定的了解,并能够安装上,进行简单的使用了,但是在咱们的 ...
- 初识 MySQL 5.6 新特性、功能
背景: 之前介绍过 MySQL 5.5 新功能.参数,现在要用MySQL5.6,所以就学习和了解下MySQL5.6新的特性和功能,尽量避免踩坑.在后续的学习过程中文章也会不定时更新. 一:参数默认值的 ...
- nyoj133_子序列_离散化_尺取法
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
- PUTTY的使用教程
Putty是一个优秀的,开源的SSH远程登录软件. 它不仅仅可以实现登录,还有很多高级功能. PuTTY is a free SSH, Telnet and Rlogin client for 32- ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- [Python] dir() 与 __dict__,__slots__ 的区别
首先需要知道的是,dir() 是 Python 提供的一个 API 函数,dir() 函数会自动寻找一个对象的所有属性,包括搜索 __dict__ 中列出的属性. 不是所有的对象都有 __dict__ ...
- 43个优秀的Swift开源项目
作为一门集百家之长的新语言,Swift拥有着苹果先天的生态优势,而其在GitHub上各种优秀的开源项目也层出不穷.本文作者@SwiftLanguage从2014年6月苹果发布Swift语言以来,便通过 ...