transaction transaction transaction

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. 

InputThe 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). 
OutputFor 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

一个人在任意点买书(消费点权),经过边(花费边权),在任意点卖书(收获点权),求最大收益。
树形结构,因为父子关系未知,所以双向建边。
dp[i][0]表示i子树中的最少买书消费,dp[i][1]表示i子树中的最大卖书收益,
dp[i][0]+dp[i][1]表示以i为最近公共父节点的最大收益,最优解并非根节点,因此ans需要不断更新。
#include<bits/stdc++.h>
#define MAX 100005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; int a[MAX];
int dp[MAX][];
int ans;
struct Node{
int v,w;
}node;
vector<Node> v[MAX]; void dfs(int x,int pre){
dp[x][]=-a[x];dp[x][]=a[x];
for(int i=;i<v[x].size();i++){
int to=v[x][i].v;
if(to==pre) continue;
int w=v[x][i].w;
dfs(to,x);
dp[x][]=max(dp[x][],dp[to][]-w);
dp[x][]=max(dp[x][],dp[to][]-w);
}
ans=max(ans,dp[x][]+dp[x][]); //不断更新
}
int main()
{
int t,n,i;
int x,y,w;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d",&a[i]);
v[i].clear();
}
for(i=;i<n;i++){
scanf("%d%d%d",&x,&y,&w);
node.v=y;node.w=w;
v[x].push_back(node);
node.v=x;
v[y].push_back(node);
}
ans=-INF;
dfs(,-);
printf("%d\n",ans);
}
return ;
}

最新文章

  1. 5-3 bash脚本编程之二 条件判断
  2. RHEL7.2
  3. 1236 - Pairs Forming LCM -- LightOj1236 (LCM)
  4. [转]acm忠告
  5. Nuget-使用图形化界面打包自己的类库
  6. iar 数据类型 int folat
  7. VS2010运行类向导提示“未实现该方法或操作”
  8. DHCP和NAT的概念与对比
  9. android中的命令安装与卸载
  10. 学学数据库,记记sql
  11. Excel常用函数大全
  12. 【ZZ】常用推荐算法
  13. Ubuntu 下升级git到最新版
  14. linux下crontab的使用实现
  15. 暑假集训2016day3T1 欧拉回路(UOJ #117欧拉回路)(史上最全的欧拉回路纯无向图/有向图解析)
  16. express学习点滴- 永远不要忘记异步
  17. 初识 Javascript.01 -- Javascript基础|输出方式、变量、变量命名规范、数据类型、
  18. Imageloader框架
  19. 处理table 超出部分滚动问题
  20. IntelliJ_2017_安装Grep Console插件(console输出内容加颜色)

热门文章

  1. struts2的 defalut-action-ref 的使用
  2. 我的Java开发学习之旅------>Java语言中方法的参数传递机制
  3. debug x86 汇编程序指南
  4. Win8+VS2012 配置OpenGL SuperBible5 环境
  5. Android RelativeLayout相对布局
  6. :style动态设置属性
  7. js作用域总结
  8. &lt;JAVA图像学习笔记&gt;十字路口交通模拟--操作系统模拟课后小项目
  9. POJ3693 Maximum repetition substring —— 后缀数组 重复次数最多的连续重复子串
  10. POJ3261 Milk Patterns —— 后缀数组 出现k次且可重叠的最长子串