练习赛补题--------E - Treasure Hunt I DP

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.

树上DP;
n个点,n-1条边,一共有m天,每条边都会有消耗的天数,又因为每次出去,必须在m天内回到原先出发的地方,因为一棵树的原因,那么它一定会原路返回,所以回程可以不需要考虑,将m天缩为m / 2天即可,
dp[u][j]代表从u点出发,有最大代价为j所能获得的最大价值;
边界值处理:dp[u][0] = val[u];(价值数)
dp[u][j] = max(dp[u][j] , dp[u][j - w[u][v] - k] + dp[v][k]);
v是u的邻接点。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

vector<int>ve[105];
int mk[105][105];
int dp[105][205];
int val[105];
int n,k,m;
int vis[105];

void dfs(int u)
{
    vis[u] = 1;
    dp[u][0] = val[u];
    int len = ve[u].size();
    for(int i = 0;i < len;++i){
        int v = ve[u][i];
        if(vis[v] == 0){
            dfs(v);
            for(int j = m;j >= 0;--j){
                for(int x = 0;x <= j - mk[u][v];++x){
                    dp[u][j] = max(dp[u][j],dp[u][j - mk[u][v] - x] + dp[v][x]);
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%d",&n)){
        for(int i = 1;i <= n;++i){
            ve[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 = 0;i < n - 1;++i){
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            ve[u].pb(v);
            ve[v].pb(u);
            mk[u][v] = mk[v][u] = w;
        }
        scanf("%d %d",&k,&m);
        m /= 2;
        memset(vis,0,sizeof(vis));
        dfs(k);
        int ans = -1;
        for(int i = 0;i <= m;++i){
            ans = max(ans,dp[k][i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值