(树形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;
}

  

 

posted @ 2015-05-17 11:43  waterfull  阅读(163)  评论(0编辑  收藏  举报