【树形DP】ZOJ 3626 Treasure Hunt I

通道:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626

题意:点权边权树,求从k出发,花费为m情况内,最终回到k点,点权和最大是多少

思路:f(i,j)表示子树i, 用花费j最多可以获得的价值
   对与i的每个儿子,可以选择分配花费2*w, 2*w+1, 2*w+2,...j给它,可以看作是一组物品
   对所有儿子做分组背包
   f(i, j) = max{ max{ f(i, j-k) + f(v, k-2*w) | 2*w<=k<=i }  | v是i的儿子节点}

代码:

 

#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
        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, w;
            scanf("%d %d %d", &u, &v, &w);
            adj[u].push_back(MP(v, w));
            adj[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;
}
View Code

 

posted @ 2014-11-03 19:51  mithrilhan  阅读(154)  评论(0编辑  收藏  举报