HDOJ1011(树形动规)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define max (a>b)?a:b
const int nMax = 105;
int map[nMax][nMax];
int d[nMax][nMax];
int visit[nMax];

struct Room
{
  int bugs, brains;
}room[nMax];
int N, M;

void dp(int u)//这里的动态规划,等于分层处理,每次处理一层。递归非递归相互配合。
{
  visit[u] = 1;
  int r = (room[u].bugs + 19) / 20;
  for(int i = M; i >= r; i --)
    d[u][i] = room[u].brains;//初始化
  for(int v = 1;v <= N; ++ v)
  {
    if(map[u][v] && !visit[v])
    {
      dp(v);
      for(int j = M; j >= r; j --)//这里需要注意,从大到小,这样可以避免自身节点的干扰。这里的d只需要处理其他子树
      {
        //这里如果想使用递归,则从M到r,执行对u的更新处理即可。
        for(int k = 1; k <= j - r; k ++)
          d[u][j] = max(d[u][j], d[u][j - k] + d[v][k]);
      }
    }
  }
}

int main()
{

  while(scanf("%d%d", &N, &M) != EOF)
  {
    if(N == -1) break;
    for(int i = 1; i <= N; ++ i)
      scanf("%d%d", &room[i].bugs, &room[i].brains);
    memset(map, 0, sizeof(map));
    for(int i = 1; i < N; ++ i)
    {
      int a, b;
      scanf("%d%d", &a, &b);
      map[a][b] = map[b][a] = 1;
    }
    if(!M) //如果M为零,需要特殊判断,特殊数据
    {
      printf("0\n");
      continue;
    }
    memset(d, 0, sizeof(d));
    memset(visit, 0, sizeof(visit));
    dp(1);
    printf("%d\n", d[1][M]);
  }
  return 0;
}
时间: 2024-08-08 00:00:55

HDOJ1011(树形动规)的相关文章

【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边权每次经过都要扣,问从每一个节点开始走最大能获得的价值. 题目思路: [树形动态规划] 首先用dfs求出从根1往下走的:节点u往下走最后回到节点u的最大值g[u],节点u往下走最后不回到u的最优值和次优值f[0][u],f[1][u] 接着考虑一个节点u,除了以上的情况还有可能是往它的父亲方向走,这

树形动规专题

由于冷大佬的毒奶,我开始刷树形dp了, 先来一到签到题:[POI2011]DYN-Dynamite 我们看到最大值最小,自然地想到二分,我们二分一个最大值,题目就转化为了一个点能覆盖mid范围内的点,求要有几个点能全部覆盖所有的特殊点.想到消防局的设立:但是这道题目又不能那么做.考虑DP,设F[i]表示 以i为根节点的子树中没有被覆盖的最远的点,g[i]表示以i为根的子树中距离i最近的已设立的点(为什么要这么操作?考虑树形dp一般是倒着跑dfs的,我们抉择要不要设立点肯定要考虑距离它最远的未被覆

[JSOI2008][BZOJ1017] 魔兽地图DotR|树形动规

1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1254  Solved: 537[Submit][Status][Discuss] Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Ancients) Allstars.DotR里面的英雄只有一个属性——力

【codevs1378】选课——树形动规

题目传送门 这道题可以用多叉树转二叉树的方法,左子树为儿子,右子树为同个父亲的兄弟,这样的话就可以很方便地写出状态转移方程: f[x][v]=max(f[x][v],w[x]+dp(ch[x][0],i)+dp(ch[x][1],v-i-1)): 要注意的是,f[x][v]可能在之前已经计算过(比如f[ch[i][0]][4]在f[i][5,6,7...]的时候就已经计算过)因此不必再计算,否则会TLE. 剩下的就不难了,直接DFS即可,具体实现细节看代码: #include<cstdio>

洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)

题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so they can all

洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)

题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of course, she would like to choose the most convenient location for the gathering to take place. Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会

POJ 2955 Brackets (动规)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2999   Accepted: 1536 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

sicily 1091 Maximum Sum (动规)

1 //1091.Maximum Sum 2 //b(i,j) = max{b(i,j-1)+a[j], max(b(i-1,t)+a[j])} (t<j) 3 #include <iostream> 4 using namespace std; 5 6 int main() { 7 int t; 8 cin>>t; 9 while (t--) { 10 int n; 11 cin>>n; 12 int a[n+1]; 13 for (int i = 1; i &

ACM/ICPC 之 经典动规(POJ1088-滑雪)

POJ1088-滑雪 将每个滑雪点都看作起点,从最低点开始逐个由四周递推出到达此点的最长路径的长度,由该点记下. 理论上,也可以将每一点都看作终点,由最高点开始计数,有兴趣可以试试. 1 //经典DP-由高向低海拔滑雪-求最长路 2 //Memory:372K Time:32 Ms 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 using