H - 因为司是一个容易让自己去忍受的人 HDU - 6201 transaction transaction transaction 树形DP

H - 因为司是一个容易让自己去忍受的人

 HDU - 6201 

Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named "the Man Who Changed China". Of course, Kelukin wouldn't miss this chance to make money, but he doesn't have this book. So he has to choose two city to buy and sell. 
As we know, the price of this book was different in each city. It is aiai yuanyuan in iittcity. Kelukin will take taxi, whose price is 11yuanyuan per km and this fare cannot be ignored. 
There are n−1n−1 roads connecting nn cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get. 

Input

The first line contains an integer TT (1≤T≤101≤T≤10) , the number of test cases. 
For each test case: 
first line contains an integer nn (2≤n≤1000002≤n≤100000) means the number of cities; 
second line contains nn numbers, the iithth number means the prices in iithth city; (1≤Price≤10000)(1≤Price≤10000) 
then follows n−1n−1 lines, each contains three numbers xx, yy and zz which means there exists a road between xx and yy, the distance is zzkmkm (1≤z≤1000)(1≤z≤1000). 

Output

For each test case, output a single number in a line: the maximum money he can get. 

Sample Input

1  
4  
10 40 15 30  
1 2 30
1 3 2
3 4 10

Sample Output

8

题意: 一本书在n个点的价格分别为pi,这n个点之间又n-1条带权边(树),选择两个点【st,en】使得从st买一本书运到en去卖收益最大(经过的每条边都要交路费)。

思路:树形DP:随意去一点作为根节点,dp[x][0]:在x点或者x的子树里选一点买书的最大收益,dp[x][1]:在x点或者x的子树里选一点卖书的最大收益,显然  dp[x][0] + dp[x][1] 即为  x及x的子树  这部分点  的答案

dp[x][0]=max(dp[x][0],dp[to][0]-tre[x][i].var);//到x买书OR到x的子节点买书,取最大收益   (to是x的第i个子节点,var是x-->i的边权值)
dp[x][1]=max(dp[x][1],dp[to][1]-tre[x][i].var);//到x卖书OR到x的子节点卖书,取最大收益

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define fora(i,a,b) for(i=a;i<b;i++)
#define fors(i,a,b) for(i=a;i>b;i--)
#define fora2(i,a,b) for(i=a;i<=b;i++)
#define fors2(i,a,b) for(i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f

typedef long long LL;
typedef long long LD;
using namespace std;
const int maxn=100000+5;
int N,M,ans;
int p[maxn];
int dp[maxn][2];
int vis[maxn];
struct node
{
    int to;
    int var;
};
vector<node>tre[maxn];
void init()
{
    memset(dp,0,sizeof(dp));
    memset(vis,0,sizeof(vis));

    ans=-INF;
}
void DFS(int x)
{
    dp[x][0]=-p[x];
    dp[x][1]=p[x];
    vis[x]=1;
    int i,len=tre[x].size();
    fora(i,0,len)
    {
        int to=tre[x][i].to;
        if(vis[to]==0)DFS(to);
        else continue;
        dp[x][0]=max(dp[x][0],dp[to][0]-tre[x][i].var);//到x买书OR到x的子节点买书
        dp[x][1]=max(dp[x][1],dp[to][1]-tre[x][i].var);//到x卖书OR到x的子节点卖书
    }
    ans=max(ans,dp[x][0]+dp[x][1]);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {

        int i;
        scanf("%d",&N);
        init();
        fora2(i,1,N)
        {
            scanf("%d",p+i);
            tre[i].clear();
        }
        fora(i,1,N)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            tre[y].push_back((node){x,z});
            tre[x].push_back((node){y,z});
        }
        DFS(1);
        printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值