HDU 6201 transaction transaction transaction(树形DP)

transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 895    Accepted Submission(s): 441


Problem Description
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 ai yuan in it city. Kelukin will take taxi, whose price is 1yuan per km and this fare cannot be ignored.
There are n1 roads connecting n 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 T (1T10) , the number of test cases. 
For each test case:
first line contains an integer n (2n100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1Price10000) 
then follows n1 lines, each contains three numbers xy and z which means there exists a road between x and y, the distance is zkm (1z1000)
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
Source

 【题意】给你一棵树,每个节点可以买卖物品,价格是w[i],边有权值,表示路长,每走以单位,会消耗1元,现在要选择一个点买物品,然后走到一个点去买,问最大收益。

【分析】树形DP,dp[u][0]表示u节点及其子树中,选个节点卖物品,然后走到u扣掉的最小花费,dp[u][10]表示u节点及其子树中,选个节点买物品,然后走到u扣掉的最小花费,然后加起来更新ans,这里可能会想到,要是两种情况取最值得时候是同一个节点呢?假设在u节点的子树中的所有节点(不包括u)中,二者取最大值时都是v节点,

即-w[v]-dis[u,v],w[v]-dis[u,v]最大,现在我们把v与u比较,假设-w[v]-dis[u,v]>=-w[u],则w[v]-dis[u,v]>=w[u]-2*dis[u,v]<w[u],与w[v]-dis[u,v]>w[u]矛盾,也就是说,二者取最大值时,是不同的点,详情见代码。

#include <bits/stdc++.h>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define met(a,b) memset(a,b,sizeof a)
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=1000000007;
const int inf=0x3f3f3f3f;
const int N = 1e5+50;
const double pi=acos(-1);
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int n,m;
int w[N];
vector<pii>edg[N];
ll dp[N][2],ans;
void dfs(int u,int f){
    dp[u][0]=w[u];dp[u][1]=-w[u];
    for(auto e : edg[u]){
        int v=e.first;
        int cost=e.second;
        if(v==f)continue;
        dfs(v,u);
        dp[u][0]=max(dp[u][0],dp[v][0]-cost);
        dp[u][1]=max(dp[u][1],dp[v][1]-cost);
    }
    ans=max(dp[u][0]+dp[u][1],ans);
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&w[i]),edg[i].clear();
        for (int i=1,a,b,c; i<n; ++i) {
            scanf("%d%d%d",&a,&b,&c);
            edg[a].pb(mp(b,c));
            edg[b].pb(mp(a,c));
        }
        ans=-100000000000;
        dfs(1,0);
        printf("%lld
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jianrenfang/p/7507635.html