Algorithm: zoj-3626 treasure Hunt I (tree dp)

Source: Internet
Author: User

Meaning

to an N-node tree, the node number 1~n, each node has the right value val[i], this value can be obtained through this node (cannot be repeated)

Each side has a cost value of W (i,j), which means that the edges of the I and J points will cost W (i,j).

Now to start with the K point, the total cost is M, ask the total cost of not more than m in the case and ultimately to return to the starting point, how much value can be obtained?

Ideas

Simple tree DP.

F (i,j) is the value of the subtree I, which can be obtained with cost J

Pairs with each son of I, which can be assigned to spend 2*w, 2*w+1, 2*w+2,... J to it, can be seen as a group of items

Pack a backpack for all the sons.

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

Ans = f (k, m);

Code

/**===================================================== * is a solution for ACM/ICPC problem * * @source: Zoj -3626 Treasure Hunt I * @description: Tree DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: Zengshuang
de@gmail.com * Copyright (C) 2013/08/26 16:23 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> # Include <algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring
    
> #include <string> #include <map> #include <set> #define MP make_pair using namespace std;
typedef pair<int, int >PII;
typedef long long Int64;
Const double PI = ACOs (-1.0);
    
const int INF = 0X3F3F3F3F;
const int MAXN = 110;
int N, K, m;
vector<pii>adj[maxn];
int VAL[MAXN];
BOOL VIS[MAXN];
    
int F[MAXN][2*MAXN];
    
void Dfs (int u) {Vis[u] = true;
    
for (int i = 0; I <= m ++i) f[u][i] = Val[u]; for (iNT e = 0; E < adj[u].size ();
++e) {int v = adj[u][e].first int w = adj[u][e].second; if (vis[v]) continue; Dfs (v); for (int i = m; I >= 0;-i) {
for (int j = 2*w J <= i; ++j) {F[u][i] = max (F[u][i], f[u][i-j] + f[v][j-2*w);} int main () {while (~SCANF ("%d", &n)) {//init//www.bianceng.cn for (int i = 0; I <= N; ++i) A
    
Dj[i].clear ();
    
for (int i = 1; I <= n; ++i) scanf ("%d", &val[i)); for (int i = 0; i < n-1 ++i) {int u, V, W; scanf ("%d%d%d", &u, &v, &w); Adj[u].push_back (MP (V, W));
Dj[v].push_back (MP (U, W));
} scanf ("%d%d", &k, &m);
memset (f, 0, sizeof (f));
memset (Vis, 0, sizeof (VIS));
DFS (k);
printf ("%d\n", F[k][m]);
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.