Algorithm: POJ 2486 Apple trees (tree-type backpack DP)

Source: Internet
Author: User
Tags min

Meaning

to a N-node tree, the node number is 1~n, the root node is 1, and each node has a weight value.

Starting from the root node, go no more than k step, ask the maximum number of weights can be obtained?

Ideas

Because it's a bit like uva-1407 caves, I didn't think about AC for a long time, but because of the initialization problem WA twice

F (i, J, 0): Represents the subtree I, walks the J times, and ultimately does not have to return to the I point to obtain the maximum total weight

F (I, J, 1): Represents the subtree I, walks the J time, finally must return to the I point obtains the maximum total weight value

F (I, j, 1) = min{min{f (i, j-k, 1) + f (V, k-2, 1) | 2<=k<j} | v is son of I node}

if (j==1)

F (i, j, 0) = min{min{f (i,j-k,1) +f (v,k,0) | 1<=k<=j} | v is son of I node}

Else

F (i, j, 0) = min{min{min{f (i,j-k,1) +f (v,k-1,0), F (i,j-k,0) +f (v,k-2,1)} | 1<=k<=j} | v is son of I node}

State transitions can be a little tricky, or better understood by the code.

Code

/**===================================================== * is a solution for ACM/ICPC problem * * @source: Poj-2 486 Apple Tree * @description: Arboreal backpack DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: ZENGSHUANGDE@GM
ail.com * Copyright (C) 2013/08/23 22:17 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> # Include <algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring
    
> #define MP make_pair using namespace std;
typedef pair<int, int >PII;
typedef long long Int64;
    
const int INF = 0X3F3F3F3F;
const int MAXN = 110;
vector<int>adj[maxn];
int VAL[MAXN];
BOOL VIS[MAXN];
int f[maxn][maxn*2][2];
int n, K;
    
int ans;
    
void Dfs (int u) {Vis[u] = true;
    
Init//http://www.bianceng.cn for (int i = 0; I <= k; ++i) f[u][i][0] = f[u][i][1] = Val[u]; DP for (int e = 0; e < adj[u].size (); ++e) {inT v = adj[u][e];
if (Vis[v]) continue;
DFS (v);
for (int i = k; I >= 1;-i) {for (int j = 1; J <= i; ++j) {if (j = = 1) {int tmp1 = f[u][i-j][1] + f[v][j-1][0];
F[u][i][0] = max (f[u][i][0], TMP1); else {int TMP1 = f[u][i-j][1] + f[v][j-1][0]; int tmp2 = f[u][i-j][0] + f[v][j-2][1]; f[u][i][0] = max (f[u][i][0), Max (
TMP1, TMP2));
F[U][I][1] = max (f[u][i][1], f[u][i-j][1] + f[v][j-2][1]);
    
int main () {while ~scanf ("%d%d", &n, &k)) {for (int i = 0; I <= N; ++i) adj[i].clear ();
    
for (int i = 1; I <= n; ++i) scanf ("%d", &val[i)); 
for (int i = 0; i < n-1 ++i) {int u, v; scanf ("%d%d", &u, &v); Adj[u].push_back (v); Adj[v].push_back (u);}
memset (Vis, 0, sizeof (VIS));
memset (f, 0, sizeof (f));
DFS (1);
printf ("%d\n", f[1][k][0]);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.