bzoj 1017 tree dp

这道题几经波折啊。

最开始和vfleaking一样,把题意理解错了,认为一个装备可能被多个装备依赖,然后想不出来,去看题解。

发现自己理解错了题意,自己想想,其实也不难想到dp[i][j][k]表示“i号节点代表的子树,用掉j的钱,给父亲预留k个自己(但还是父亲付钱)”的状态,写出来交上去就是T,

开始以为是常数问题,优化半天还是T,看了他人AC代码,才发现自己算法有一定问题:重复计算了很多东西。

树形动规,一般在大的树规下,每个节点还会对儿子们来一次”资源分配“,一般是用背包解决,这道题也是这样,但又有一点不一样,就是我们也要给该子树的根节点本身分配资源,

我就是这儿重复计算了,对于所有儿子的同一k,我算了多次。

  1 /**************************************************************
  2     Problem: 1017
  3     User: idy002
  4     Language: C++
  5     Result: Accepted
  6     Time:16524 ms
  7     Memory:48316 kb
  8 ****************************************************************/
  9
 10 #include <cstdio>
 11 #include <cstring>
 12 #include <vector>
 13 #define max(a,b) ((a)>(b)?(a):(b))
 14 #define min(a,b) ((a)<(b)?(a):(b))
 15 #define maxn 55
 16 #define maxm 2010
 17 #define maxl 110
 18 #define oo 0x3f3f3f3f
 19 using namespace std;
 20
 21
 22 int n, m;
 23 int indgr[maxn];
 24 int power[maxn], cost[maxn], limit[maxn];
 25 int head[maxn], next[maxn], dest[maxn], wght[maxn], tot;
 26
 27 int dp[maxn][maxm][maxl];
 28 int ep[maxm];
 29
 30 void insert( int a, int b, int w ) {
 31     tot++;
 32     wght[tot] = w;
 33     dest[tot] = b;
 34     next[tot] = head[a];
 35     head[a] = tot;
 36 }
 37 void dfs( int i ) {
 38     if( !head[i] ) return;
 39     cost[i] = 0;
 40     limit[i] = oo;
 41
 42     for( int t=head[i]; t; t=next[t] ) {
 43         int s = dest[t], w = wght[t];
 44         dfs( s );
 45         cost[i] += cost[s]*w;
 46         limit[i] = min( limit[i], limit[s]/w );
 47     }
 48 }
 49
 50
 51 void dodp( int i ) {
 52     if( !head[i] ) {
 53         for( int l=0; l<=limit[i]; l++ )
 54             for( int k=l; k>=0; k-- ) {
 55                 int self = (l-k);
 56                 int j = self*cost[i];
 57                 if( j>m ) break;
 58                 dp[i][j][k] = self*power[i];
 59             }
 60         return;
 61     }
 62     for( int t=head[i]; t; t=next[t] )
 63         dodp( dest[t] );
 64     for( int l=0; l<=limit[i]; l++ ) {
 65         for( int j=0; j<=m; j++ ) ep[j]=0;
 66         for( int t=head[i]; t; t=next[t] ) {
 67             int v = dest[t], w = wght[t];
 68             int vl = l*w;
 69             for( int j=m; j>=0; j-- )
 70                 for( int jj=0; jj<=j; jj++ )
 71                     ep[j] = max( ep[j], ep[j-jj]+dp[v][jj][vl] );
 72         }
 73         for( int j=0; j<=m; j++ )
 74             for( int k=l; k>=0; k-- ) {
 75                 int self = l-k;
 76                 int remain =  j- self*cost[i];
 77                 if( remain<0 ) break;
 78                 dp[i][j][k] = max( dp[i][j][k], ep[remain]+self*power[i] );
 79             }
 80     }
 81 }
 82
 83 int main() {
 84     scanf( "%d%d", &n, &m );
 85     for( int i=1; i<=n; i++ ) {
 86         char type[10];
 87         scanf( "%d%s", power+i, type );
 88         if( type[0]==‘A‘ ) {
 89             int cnt;
 90             scanf( "%d", &cnt );
 91             for( int t=1,c,w; t<=cnt; t++ ) {
 92                 scanf( "%d%d", &c, &w );
 93                 insert( i, c, w );
 94                 indgr[c]++;
 95             }
 96         } else
 97             scanf( "%d%d", cost+i, limit+i );
 98     }
 99     int root = 0;
100     for( int i=1; i<=n; i++ )
101         if( indgr[i]==0 ) {
102             root = i;
103             break;
104         }
105     memset( dp, 129, sizeof(dp) );
106     dfs(root);
107     dodp(root);
108     int ans = 0;
109     for( int j=0; j<=m; j++ ) ans = max( ans, dp[root][j][0] );
110     printf( "%d\n", ans );
111 }

时间: 2024-08-27 12:58:00

bzoj 1017 tree dp的相关文章

HDU 4359 Easy Tree DP?

Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 567 Problem Description A Bear tree is a binary tree with such properties : each node has a value o

hdu5534 Partial Tree dp(难)

hdu5534 Partial Tree  dp(难) Partial Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 374    Accepted Submission(s): 209 Problem Description In mathematics, and more specifically in graph t

HDU 4359——Easy Tree DP?——————【dp+组合计数】

Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1460    Accepted Submission(s): 557 Problem Description A Bear tree is a binary tree with such properties : each node has a value o

codeforces 514E Darth Vader and Tree (dp+快速幂)

codeforces 514E Darth Vader and Tree (dp+快速幂) 题意: 有一棵树,每个节点有n个儿子,给出父亲到每个儿子的距离di,问离祖先距离不超过x的子孙有多少个(子孙包括祖先)对1e9+7取模. 限制: 1 <= n <= 1e5; 0 <= x <= 1e9; 1 <= di <= 100 思路: 因为di <= 100,所以可以用快速幂来处理这道题, 大概过程为:先用dp算出前100的答案,剩下的用快速幂来处理.

TYOI Day1 travel:Tree dp【处理重复走边】

题意: 给你一棵树,n个节点,每条边有长度. 然后有q组询问(u,k),每次问你:从节点u出发,走到某个节点的距离mod k的最大值. 题解: 对于无根树上的dp,一般都是先转成以1为根的有根树,然后分别从上到下和从下到上两遍dp. 另一个技巧是:处理重复走边的情况时,可以让dp值表示达到某种状态的方案数. 表示状态: dp[i][j][k] = max dis 表示从i节点出发,走的距离mod k = j时的方案数 找出答案: 对于每次询问(u,k),答案为:满足dp[u][d][k]>0的最

bzoj 1017[JSOI2008]魔兽地图DotR - 树形dp

1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec  Memory Limit: 162 MB Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Ancients) Allstars.DotR里面的英雄只有一个属性--力量.他们需要购买装备来提升自己的力量值,每件装备都可以使佩戴它的英雄的力量值提高固定的点数,所以英雄

BZOJ 1017 魔兽地图DotR(树形DP)

题意:有两类装备,高级装备A和基础装备B.现在有m的钱.每种B有一个单价和可以购买的数量上限.每个Ai可以由Ci种其他物品合成,给出Ci种其他物品每种需要的数量.每个装备有一个贡献值.求最大的贡献值.已知物品的合成路线是一个严格的树模型.即有一种物品不会合成其他任意物品,其余物品都会仅仅可用作合成另外一种物品 思路:f[i][j][k],代表第i个物品,花费了j,向上提供k个i物品. 1 #include<algorithm> 2 #include<cstdio> 3 #inclu

[BZOJ 1017][JSOI2008]魔兽地图DotR(树形Dp)

Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Ancients) Allstars.DotR里面的英雄只有一个属性——力量.他们需要购买装备来提升自己的 力量值,每件装备都可以使佩戴它的英雄的力量值提高固定的点数,所以英雄的力量值等于它购买的所有装备的力 量值之和.装备分为基本装备和高级装备两种.基本装备可以直接从商店里面用金币购买,而高级装备需要用

BZOJ 1017 魔兽地图

Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Ancients) Allstars.DotR里面的英雄只有一个属性——力量.他们需要购买装备来提升自己的力量值,每件装备都可以使佩戴它的英雄的力量值提高固定的点数,所以英雄的力量值等于它购买的所有装备的力量值之和.装备分为基本装备和高级装备两种.基本装备可以直接从商店里面用金币购买,而高级装备需要用基本