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 n−1 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 (1≤T≤10) , the number of test cases. 
For each test case:
first line contains an integer n (2≤n≤100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1≤Price≤10000) 
then follows n−1 lines, each contains three numbers x, y and z which means there exists a road between x and y, the distance is zkm (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
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=;
const int inf=0x3f3f3f3f;
const int N = 1e5+;
const double pi=acos(-);
ll powmod(ll a,ll b) {ll res=;a%=mod; assert(b>=); for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
int n,m;
int w[N];
vector<pii>edg[N];
ll dp[N][],ans;
void dfs(int u,int f){
dp[u][]=w[u];dp[u][]=-w[u];
for(auto e : edg[u]){
int v=e.first;
int cost=e.second;
if(v==f)continue;
dfs(v,u);
dp[u][]=max(dp[u][],dp[v][]-cost);
dp[u][]=max(dp[u][],dp[v][]-cost);
}
ans=max(dp[u][]+dp[u][],ans);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&w[i]),edg[i].clear();
for (int i=,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=-;
dfs(,);
printf("%lld\n",ans);
}
return ;
}

HDU 6201 transaction transaction transaction(树形DP)的更多相关文章

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  3. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  4. hdu 4003 Find Metal Mineral 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...

  5. HDU 5758 Explorer Bo(树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5758 [题目大意] 给出一棵树,每条路长度为1,允许从一个节点传送到任意一个节点,现在要求在传送次 ...

  6. 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. HDU 4123 Bob’s Race 树形dp+单调队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...

  8. HDU 4799 LIKE vs CANDLE 树形dp

    题意:有n个人,他们的关系,形成一棵有根树(0是树根,代表管理员),每个人有一个价值 现在有一条微博,每个人要么点赞,要么送一个蜡烛 初始一些人利用bug反转了某些人的操作(赞变蜡烛 或者 蜡烛变成赞 ...

  9. hdu 3586 Information Disturbing(树形dp + 二分)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:   hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...

  10. HDU 1054 Strategic Game(树形DP)

    Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he ...

随机推荐

  1. 【BZOJ】4129: Haruna’s Breakfast 树分块+带修改莫队算法

    [题意]给定n个节点的树,每个节点有一个数字ai,m次操作:修改一个节点的数字,或询问一条树链的数字集合的mex值.n,m<=5*10^4,0<=ai<=10^9. [算法]树分块+ ...

  2. 【洛谷P2015】二叉苹果树

    题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...

  3. 【CF802C】 Heidi and Library (hard)(费用流)

    题目链接 感觉跟餐巾计划问题有点像.费用流. 决定每天买不买不太好搞,不如先把所有东西都买进来,再卖掉不必要的. 拆点,每个点拆成\(x,y\). 源点向每个点的\(x\)连费用为当天的价格,流量为1 ...

  4. 可怕的npm蠕虫

    https://hackernoon.com/im-harvesting-credit-card-numbers-and-passwords-from-your-site-here-s-how-9a8 ...

  5. Python面向对象学习 1 (什么是面向对象,面向对象的应用场景,待更新)

    程序设计的三种基本结构: 面向对象,面向过程,函数式编程   1,什么是面向对象编程    面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就 ...

  6. 蓝色的oa模板html_综合信息服务管理平台OA模板——后台

    链接:http://pan.baidu.com/s/1qXGGOAK 密码:2otu

  7. 超级ping(多线程版)

    发现学校公共wifi的ip段是10.1.0-255.0-255段的,还是之前的思路批量ping一波. 其实可以使用nmap的.但是脚本写都写了.是吧.你懂的. #!/usr/bin/env pytho ...

  8. PHP深浅拷贝

    举个栗子: <?php class Example1 { public $name; public function __construct($name) { $this->name = ...

  9. 121.Best Time to Buy and Sell Stock---dp

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目大意:给出一串数组,找到差值最大的差 ...

  10. Class.this 和 this 的有什么不同

    转载:http://www.cnblogs.com/liwei45212/archive/2013/04/17/3026364.html 在阅读Java代码的時候 我们时会看到Class.this的使 ...