poj-2486-Apple Tree

來源:互聯網
上載者:User

標籤:div   ssi   not   day   ref   return   scanf   from   pig   

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 111 23 20 1 21 21 3

Sample Output

112

Source

POJ Contest,Author:[email protected]

 

 

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\n", ans );} return 0; }

  

 

poj-2486-Apple Tree

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.