ZOJ 3626 Treasure Hunt I 树上DP

E - Treasure Hunt I

Time Limit:2000MS
Memory Limit:65536KB

Description

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. "V1V2 ... 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. "ijTi" 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.

题意

简单来说,这道题是给你一颗树,然后每个点有一个价值,每个边有一个代价,然后问你,从k点出发,花费最多m/2的代价,能够取得最多的价值是多少。

题解

这道题实际上是一个树上背包问题,dp[i][j]表示从i点出发,花费j的代价所能取得的最大价值是多少。

转移方程为 dp[i][j]=max(dp[i][j],dp[i][m-k-t[i][v]]+dp[v][k])

跑一发就好!

吐槽

markdown这个编辑器真TM难用!

#define N 105
int val[N];
vector<int> adj[N*2];
int w[N][N];
int dp[N][N],vis[N],m;
void dfs(int u)
{
    vis[u]=1;
    dp[u][0]=val[u];
    for(int i=0;i<adj[u].size();i++)
    {
        int v=adj[u][i];
        if(vis[v]==0)
        {
            dfs(v);
            for(int j=m;j>=0;j--)
            {
                for(int k=0;k<=j-w[u][v];k++)
                {
                    dp[u][j]=max(dp[u][j],dp[u][j-k-w[u][v]]+dp[v][k]);
                }
            }
        }
    }
}
int main()
{
    int n,a,b,c,k;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        adj[i].clear();
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&val[i]);
            dp[i][0]=val[i];
        }
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            adj[a].push_back(b);
            adj[b].push_back(a);
            w[a][b]=w[b][a]=c;
        }
        scanf("%d%d",&k,&m);
        m/=2;
        memset(vis,0,sizeof(vis));
        dfs(k);
        int ans=-1;
        for(int i=0;i<=m;i++)
        {
            if(dp[k][i]>ans)
            ans=dp[k][i];
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(ZOJ)