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. dp ZOJ 3956

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 Course Selection System Time Limit ...

  2. 最小割dp Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E

    http://codeforces.com/contest/724/problem/E 题目大意:有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i<j,可以 ...

  3. 重构改善既有代码设计--重构手法12:Extract Class (提炼类)

    某个类做了应该由2个类做的事.建立一个新类,将相关的字段和函数从旧类搬移到新类. 动机:一个类应该是一个清楚地抽象,处理一些明确的责任.但是在实际工作中,类会不断成长扩展.你会在这儿加入一些功能,在哪 ...

  4. 【CodeForces】671 B. Robin Hood

    [题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...

  5. mysql 使用shell时出现 ERROR 2006 (HY000): MySQL server has gone away 解决方法

    ERROR (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection Current d ...

  6. 使sqoop能够启用压缩的一些配置

    在使用sqoop 将数据库表中数据导入至hdfs时 配置启用压缩 hadoop 的命令    检查本地库支持哪些  bin/hadoop checknative 需要配置native    要编译版本 ...

  7. Django之利用ajax实现图片预览

    利用ajax实现图片预览的步骤为: 程序实现的方法为: 方法一: upload.html <!DOCTYPE html> <html lang="en"> ...

  8. VueJS $refs 在 ElementUI 中遇到的问题

    表单验证的时候  $refs 拿不到 暂且是用 $nextTick 解决,具体原因有待研究 假入在 created 中注册时间来验证 validate,那就放在mounted中 或者...注册了 ev ...

  9. 四. Jmeter--JDBC 请求

    一,  SQLserver 1.下载 JDBC Driver (sqljdbc_6.0.8112.100_enu.exe) https://www.microsoft.com/en-us/downlo ...

  10. 新装linux系统最基本设置

    1,dns设置:vi /etc/resolv.conf   添加内容:nameserver 8.8.8.8 2,vi设置: vi ~/.bashrc  添加内容:alias vi='vim': 然后执 ...