【2017沈阳网络赛】1008 hdu6201 transaction transaction transaction 树形dp

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  i t  city. Kelukin will take taxi, whose price is  1 yuan  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  i th  number means the prices in  i th  city;  (1Price10000)  
then follows  n1  lines, each contains three numbers  x y  and  z  which means there exists a road between  x  and  y , the distance is  z km   (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
 

题意:

给出一棵树,每个点都有点权,问取两个点,使得T-S-sumw为最大(T为终点的点权,S为起点的点权,sumw为S到T的路径长度),可取相同的点。


思路:

树形dp,设d[t][0]为节点t的最大值,d[t][1]为最小值。


//
//  main.cpp
//  1008
//
//  Created by zc on 2017/9/10.
//  Copyright © 2017年 zc. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
using namespace std;
const int N=110000;
int n,a[N],d[N][2],ans;
vector<pair<int,int> >r[N];

void dfs(int t,int fa)
{
    int mmax=a[t],mmin=a[t];
    for(int i=0;i<r[t].size();i++)
    {
        int j=r[t][i].first;
        if(j==fa)   continue;
        dfs(j,t);
        if(mmax<d[j][0]-r[t][i].second) mmax=d[j][0]-r[t][i].second;
        if(mmin>d[j][1]+r[t][i].second) mmin=d[j][1]+r[t][i].second;
    }
    ans=max(ans,mmax-mmin);
    d[t][0]=mmax;
    d[t][1]=mmin;
}

int main(int argc, const char * argv[]) {
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)   scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)   r[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            r[x].push_back(make_pair(y,z));
            r[y].push_back(make_pair(x,z));
        }
        ans=0;
        memset(d,0,sizeof(d));
        dfs(1,-1);
        printf("%d\n",ans);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值