HDU 4616 Game (树形DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4616

 

题意:一颗有n个节点的树,每个节点有val值和c(有无陷阱)值,给出最大可踩陷阱数m。在树中任取一点作为起点,经过某点就取得该点的val值,踩到第m个陷阱后马上停止,而且不能走已走过的点,求所能获得的最大val总值。

 

思路:比赛时对每个点作为起点写了个爆搜,队友说如果某点u相邻的两点都没有陷阱,那么这点作为起点一定不是最优解,所以可以剪枝,可能是数据水所以过了。。。

赛后搜题解看到了以边作为第一维进行DP的思想:

dp[i][c]代表经过i这条边的儿子,再经过c个陷阱所能获得的最大val值

 

 

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <utility>
#include <functional>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <map>
#pragma comment (linker, "/STACK:1024000000,1024000000")

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

using namespace std;

const int maxn = 50010;

int n, c, cnt, head[maxn], val[maxn], ha[maxn];
int ans, tot[maxn];

struct edge
{
    int nxt, to;
} e[maxn << 1];

void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
    memset(tot, 0, sizeof(tot));
}

void add(int u, int v)
{
    e[cnt].to = v;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

void dfs(int u, int fa, int dep, int num)
{
    if(dep > c) return ;
    if(dep == c)
    {
        ans = max(ans, num);
        return ;
    }
    ans = max(ans, num);
    for(int i = head[u]; ~i; i = e[i].nxt)
    {
        int v = e[i].to;
        if(v == fa) continue;
        dfs(v, u, ha[v] + dep, num + val[v]);
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        init();
        scanf("%d%d", &n, &c);
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &val[i], &ha[i]);
        }
        for(int i = 1; i <= n - 1; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            add(u, v);
            add(v, u);
            if(!ha[u]) tot[v]++;
            if(!ha[v]) tot[u]++;
        }
        ans = 0;
        for(int i = 0; i < n; i++)
            if(tot[i] < 2)
                dfs(i, -1, ha[i], val[i]);
        printf("%d\n", ans);
    }
    return 0;
}

 

 

 

 

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <utility>
#include <functional>
#include <string>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

const int maxn = 50010;

int cnt, head[maxn];
int n, c, val[maxn], ha[maxn];
int dp[maxn << 1][10];

struct edge
{
    int  from, to, nxt;
} e[maxn << 1];

void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
    memset(dp, -1, sizeof(dp));
}

void add(int u, int v)
{
    e[cnt].from = u;
    e[cnt].to = v;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

int dfs(int id, int dep)
{
	if(dep == c) return 0;
	if(dp[id][dep] != -1) return dp[id][dep];

	int fa = e[id].from, u = e[id].to;
	int res = val[u], ddep = dep + ha[u];
	for(int i = head[u]; ~i; i = e[i].nxt)
	{
		int v = e[i].to;
		if(v == fa) continue;
		res = max(res, dfs(i, ddep) + val[u]);
	} 
	return dp[id][dep] = res;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        init();
        scanf("%d%d", &n, &c);
        for(int i = 0; i < n; i++)
        	scanf("%d%d", &val[i], &ha[i]);
        for(int i = 1; i < n; i++)
        {
        	int u, v;
        	scanf("%d%d", &u, &v);
        	add(u, v);
        	add(v, u);
        }

        int ans = 0;
        for(int i = 0; i < cnt; i++)
        {
        	int u = e[i].from;
        	ans = max(ans, dfs(i, ha[u]) + val[u]);
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值