zoukankan      html  css  js  c++  java
  • poj-2486-Apple Tree

    poj-2486-Apple Tree

    Apple Tree
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11210   Accepted: 3774

    Description

    Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the nodes she reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn’t allow Wshxzt to go more than K steps in the tree. It costs one step when she goes from one node to another adjacent node. Wshxzt likes apple very much. So she wants to eat as many as she can. Can you tell how many apples she can eat in at most K steps.

    Input

    There are several test cases in the input 
    Each test case contains three parts. 
    The first part is two numbers N K, whose meanings we have talked about just now. We denote the nodes by 1 2 ... N. Since it is a tree, each node can reach any other in only one route. (1<=N<=100, 0<=K<=200) 
    The second part contains N integers (All integers are nonnegative and not bigger than 1000). The ith number is the amount of apples in Node i. 
    The third part contains N-1 line. There are two numbers A,B in each line, meaning that Node A and Node B are adjacent. 
    Input will be ended by the end of file. 

    Note: Wshxzt starts at Node 1.

    Output

    For each test case, output the maximal numbers of apples Wshxzt can eat at a line.

    Sample Input

    2 1 
    0 11
    1 2
    3 2
    0 1 2
    1 2
    1 3
    

    Sample Output

    11
    2
    

    Source

    POJ Contest,Author:magicpig@ZSU
    17857578   2486 Accepted 892K 63MS G++ 1191B 2017-11-18 19:26:17

    树状DP。

    本题的关键在于建立dp转化方程。

      dp[x][j][0] = max( dp[x][j][0], dp[x][j-t][1] + dp[y][t-1][0] );

      表示的是不回到当前x点的话,是回到j - t 步的x当前点 加上 不回到 y 点的步骤

     
      dp[x][j][0] = max(dp[x][j][0], dp[x][j-t][0] + dp[y][t-2][1] );

      表示的 不回到当前x点。 是 不回到当前 x 点 和 回到y点的 之和。


      dp[x][j][1] = max(dp[x][j][1], dp[x][j-t][1] + dp[y][t-2][1] );

      表示回到当前x点, 是回到x点和回到子结点y的总和。

    // poj-2486  
    
    #include <cstdio> 
    #include <cstring> 
    
    
    #include <iostream> 
    #include <vector> 
    using namespace std; 
    
    const int MAXN = 100 + 10; 
    
    int n, k, val[MAXN], vis[MAXN], dp[MAXN][2*MAXN][2]; 
    
    vector<int> vt[MAXN]; 
    
    void dfs(int x){
    	vis[ x ] = 1;  
    	for(int i=0; i<vt[x].size(); ++i){
    		int y = vt[x][i]; 
    		if(vis[ y ] == 1){
    			continue; 
    		} 
    		dfs(y); 
    		for(int j=k; j>=1; --j){
    			for(int t=1; t<=j; ++t){
    				dp[x][j][0] = max( dp[x][j][0], dp[x][j-t][1] + dp[y][t-1][0] ); 
    				if(t >= 2){
    					dp[x][j][0] = max(dp[x][j][0], dp[x][j-t][0] + dp[y][t-2][1] ); 
    					dp[x][j][1] = max(dp[x][j][1], dp[x][j-t][1] + dp[y][t-2][1] ); 
    				}
    			}
    		} 
    	}
    }
    
    int main(){
    	freopen("in.txt", "r", stdin); 
    
    	int a, b, ans;  
    	while(scanf("%d %d", &n, &k) != EOF){
    		memset(dp, 0, sizeof(dp)); 
    		memset(vis, 0, sizeof(vis)); 
    
    		for(int i=1; i<=n; ++i){
    			scanf("%d", &val[i]); 
    
    			for(int j=0; j<=k; ++j){
    				dp[i][j][0] = dp[i][j][1] = val[i]; 
    			}
    		}
    
    		for(int i=1; i<=n; ++i){
    			vt[i].clear(); 
    		}
    
    		for(int i=1; i<n; ++i){
    			scanf("%d %d", &a, &b); 
    			vt[a].push_back(b); 
    			vt[b].push_back(a); 
    		} 
    
    		dfs(1); 
    
    		ans = max(dp[1][k][0], dp[1][k][1]); 
    
    		printf("%d
    ", ans );
    	} 
    	return 0; 
    }
    

      

  • 相关阅读:
    Linux下命令行安装weblogic10.3.6
    11g新特性:Health Monitor Checks
    Oracle/PLSQL: ORA-06550
    DBMS_NETWORK_ACL_ADMIN
    【RDA】使用RDA(Remote Diagnostic Agent)工具对数据库进行健康检查
    ORA-39242 错误
    Yii2 中常用的增删改查操作总结
    PHP递归函数return返回null的问题
    PHP中生成随机字符串,数字+大小写字母随机组合
    使用layer.msg 时间设置不起作用
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/7857718.html
Copyright © 2011-2022 走看看