专题训练

 #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 ****的更多相关文章

  1. hdu 4050 2011北京赛区网络赛K 概率dp ***

    题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...

  2. hdu 4049 2011北京赛区网络赛J 状压dp ***

    cl少用在for循环里 #include<cstdio> #include<iostream> #include<algorithm> #include<cs ...

  3. hdu 4045 2011北京赛区网络赛F 组合数+斯特林数 ***

    插板法基础知识 斯特林数见百科 #include<iostream> #include<cmath> #include<cstdio> #include<cs ...

  4. hdu 4043 2011北京赛区网络赛D 概率+大数 **

    推出公式为:P = A(2n,n)/(2^(2n)*n!) 但是不会大数,学完java再补

  5. hdu 4041 2011北京赛区网络赛B 搜索 ***

    直接在字符串上搜索,注意逗号的处理 #include<cstdio> #include<iostream> #include<algorithm> #include ...

  6. hdu 4046 2011北京赛区网络赛G 线段树 ***

    还带这么做的,卧槽,15分钟就被A了的题,居然没搞出来 若某位是1,则前两个为wb,这位就是w #include<cstdio> #include<cstring> #defi ...

  7. hdu 4035 2011成都赛区网络赛E 概率dp ****

    太吊了,反正我不会 /* HDU 4035 dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点 ...

  8. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  9. hdu 4031 2011成都赛区网络赛A题 线段树 ***

    就是不知道时间该怎么处理,想了好久,看了别人的题解发现原来是暴力,暴力也很巧妙啊,想不出来的那种  -_-! #include<cstdio> #include<iostream&g ...

随机推荐

  1. Java读取mat文件

    概述 使用ujmp中的jmatio模块读取.mat文件到java程序中. 其实,ujmp主要是在模块core中实现了矩阵运算,其余模块都是复用了已有的开源库.模块jmatio是复用了已有的JMatIo ...

  2. 文件上传(js, C#)

    ajaxFileUpload http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html

  3. Linux之图形化shell------dialog

    转自:Linux dialog详解(图形化shell) | 运维生存时间 对话 UNIX: 使用 shell 脚本创建好的图形应用程序---http://www.ibm.com/developerwo ...

  4. pgpool介绍和安装经验

    Pgpool的介绍 一.介绍 是一个工作在PostgreSQL多服务器和PostgreSQL数据库客户端之间的中间件. 二.概念图 三.功能 连接池:pgpool -Ⅱ保存 连 接到PostgreSQ ...

  5. Windows 下的 MarkdownPad 2 工具使用

    MarkdownPad 2 工具(windows) 一. 软件下载和安装 下载登陆官网: http://markdownpad.com/ 点击Download,会自动下载.或者直接点击http://m ...

  6. Mac shell 添加VPN 路由

    苦于每次使用内网VPN,都需要手动添加route来设置网络的映射,才可以正常使用VPN. 每次都需要自动添加,当然就是觉得很烦,想到Mac的shell.反正每次添加的时候都是在终端执行添加路由.为了方 ...

  7. plsql客户端显示菜单等

    不小心把plsql的左边的都关了,如图 菜单条---工具---浏览器. 即可.

  8. sqlserver insert 存储过程

    -- 根据表中数据生成insert语句的存储过程Create Proc proc_insert (@tablename varchar(256))  as                        ...

  9. 【python】f.write()写入中文出错解决办法

    一个出错的例子 #coding:utf-8 s = u'中文' f = open("test.txt","w") f.write(s) f.close() 原因 ...

  10. WebFrom 的js日期控件

    1.WdatePicker.js /* * My97 DatePicker 4.7 Release * License: http://www.my97.net/dp/license.asp */ v ...