(树形DP) zoj 3626

Treasure Hunt I


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Akiba is a dangerous country since a bloodsucker living there. Sometimes the bloodsucker will appear and kill everyone who isn‘t at his hometown. One day, a brave person named CC finds a treasure map, and he wants to get as much as possible.

Akiba consists of n towns and n-1 roads. There is a way from each town to any other. Each town contains some treasure values Vi. CC starts from town k(his hometown), at day 0. After m days, the bloodsucker will appear and CC would be killed if he hasn‘t been back yet, it means CC has m days for hunting the treasure at most. It takes CC Ti days to move from one town to another neighbour town.(Two towns called neighbour if they are the endpoint of one road.) You can assume CC will get the treasure immediately as he arrives at that town. CC wants to obtain as much value as possible, keeping him alive at the same time.

Input

There are multiple cases, about 50 cases.
The first line of each case contains an integer n, indicating there are n towns.
The following line describe the treasure‘s value in each town. "V1 V2 ... Vn". Vi is the value of the treasure in ith town. Each value is separated by one blank.
The next n-1 lines describe the n-1 roads in Akiba. "i j Ti" Means the ith town and the jth town are endpoints of that road. It takes Ti days to get through this road.
The last line has two integer k and m as described above.

1<=n<=100, 0<=Vi<=1000 , 1<=Ti<=10
1<=k<=n, 1<=m<=200
All the inputs are integers.

Output

Just output the max value CC can get, and you should keep CC alive after m days.

Sample Input

2
1 3
1 2 1
1 2
2
1 3
2 1 1
2 1
2
3 3
1 2 1
2 5

Sample Output

4
3
6

Hint

Sample 1: CC can go to town 2 and return at day 2.
Sample 2: CC can‘t come back within 1 day. So he can only take the treasure in his hometown.
Sample 3: CC only need 2 days to collect all the treasure.

题意:

给一棵n个节点的树, 节点编号1~n, 每个节点有权值val[i],经过这个节点就可以获取这个价值(不能重复获得)
   每一条边有一个花费值w(i,j), 表示走完i和j点的边要花费w(i,j)
   现在要从k点出发,总花费值为m,问总花费不超过m的情况下并且最终要回到出发点,最多可以获取多少价值?

dp[k][j]从k出发花费j元获得的最大价值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> e[105];
int n,val[105],dp[105][105],w[105][105],k,m;
void dfs(int u,int father)
{
    dp[u][0]=val[u];
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(v==father)
            continue;
        dfs(v,u);
        for(int j=m;j>=0;j--)
        {
            for(int t=0;t<=j-w[u][v];t++)
            {
                dp[u][j]=max(dp[u][j],dp[v][t]+dp[u][j-t-w[u][v]]);
            }
        }
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            e[i].clear();
        memset(w,0,sizeof(w));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            scanf("%d",&val[i]);
        for(int i=1;i<n;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            e[x].push_back(y);
            e[y].push_back(x);
            w[x][y]=z;
            w[y][x]=z;
        }
        scanf("%d%d",&k,&m);
        m=m/2;
        dfs(k,-1);
        int ans=0;
        for(int i=0;i<=m;i++)
        {
            if(dp[k][i]>ans)
                ans=dp[k][i];
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-11-07 14:48:37

(树形DP) zoj 3626的相关文章

(树形DP) zoj 3201

Tree of Tree Time Limit: 1 Second      Memory Limit: 32768 KB You're given a tree with weights of each node, you need to find the maximum subtree of specified size of this tree. Tree Definition A tree is a connected graph which contains no cycles. In

ZOJ 3626(树形DP+背包+边cost)

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回到起点.给定预算m,问最大价值. 解题思路: 首先要注意这题要回到起点,由于树的特殊结构(每个结点只有一个父亲)也就是说,要回到开头, 开销是2倍.所以首先m/=2. 然后就是树形背包的求解,这题的cost在边,所以for写法变成如下: for(m....j....0)     for(0....k

zoj 3626 Treasure Hunt I (树形dp)

题目大意: 给出一棵树,求出从起点开始走m长度最后回到起点,所能得到的宝藏的最大价值. 思路分析: 通过一次dfs可以得到的是子树到根节点的所有距离的最大值. 现在的问题就是他走完一颗子树可以去另外一颗子树. 所以在回溯到根的时候要统计其他子树上互补距离的最大值. dp[i] [j] 表示i为根节点,在i的子树中走j步然后回到i所能拿到的最大价值. 转移方程就是 dp[x][i+2*len]=max(dp[x][i+2*len],dp[v][j]+dp[x][i-j]); v为x的子树的根,le

[zoj 3626]Treasure Hunt I 树DP

<span style="font-family: Arial, Helvetica, Verdana, sans-serif; background-color: rgb(255, 255, 255);">Treasure Hunt I</span> Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since a bloodsucker living

2014 Super Training #9 E Destroy --树的直径+树形DP

原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其他点的最远距离最小).现在你要破坏所有叶子节点到根节点的连通,每条边破坏都需要一定能量.你有一个能量为power的武器,能破坏能量小于等于power的任何路.求最少需要的power. 解法参考博客:http://blog.csdn.net/gzh1992n/article/details/86511

【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock&#39;s blog】

树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AVL树,线段树.SPLAY树,后缀树等等.. 枚举那么多种数据结构只是想说树方面的内容相当多,本专辑只针对在树上的动态规划,即树形DP.做树形DP一般步骤是先将树转换为有根树,然后在树上进行深搜操作,从子节点或子树中返回信息层层往上更新至根节点.这里面的关键就是返回的信息部分,这个也没一般性的东西可讲

HDU-2196 Computer (树形DP)

最近在看树形DP,这题应该是树形DP的经典题了,写完以后还是有点感觉的.之后看了discuss可以用树分治来做,以后再试一试. 题目大意 找到带权树上离每个点的最远点.︿( ̄︶ ̄)︿ 题解: 对于每一个点的最远点,就是以这个点为根到所有叶子节点的最长距离.但是如果确定根的话,除了根节点外,只能找到每个节点(度数-1)个子树的最大值,剩下一个子树是该节点当前的父亲节点. 所以当前节点的最远点在当前节点子树的所有叶子节点以及父亲节点的最远点上(当父亲节点的最远点不在当前节点的子树上时), 如果父亲节

UVA-01220 Party at Hali-Bula (树形DP+map)

题目链接:https://vjudge.net/problem/UVA-1220 思路: 树形DP模板题,求最大人数很简单,难点在于如何判断最大人数的名单是否有不同的情况: 解决方法是用一个数组f[manx][2]记录该节点是否出场的情况,为真时代表有多种情况; 具体讨论: 当父节点的值加上某个子节点的值时,他的f的情况也和该子节点一样: 当某个节点dp(i, 0) == dp(i, 1), 则该节点以及它的父节点也一定有多种情况(父节点必定取其中之一). Code: 1 #include<bi

HDU 1520 树形dp裸题

1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b using nam