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 ;
}

HDU - 6201 transaction transaction transaction(树形dp取两点)的更多相关文章

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

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

  2. hdu 4514 并查集+树形dp

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

  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.每条边有权值,砍掉一条边要花费 ...

随机推荐

  1. Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)

    sftp -b batchfile username@remote_host 报错:Permission denied (publickey,gssapi-keyex,gssapi-with-mic, ...

  2. NotePad++ 正则表达式替换

    NotePad++ 正则表达式替换 高级用法 [转] - aj117 - 博客园 https://www.cnblogs.com/tibit/p/6387199.html const getQAPar ...

  3. Android 虚拟机 程序安装目录

    Android应用安装涉及到如下几个目录:system/app系统自带的应用程序,无法删除.data/app用户程序安装的目录,有删除权限.安装时把apk文件复制到此目录.data/data存放应用程 ...

  4. Apache NiFi 开发 处理器使用说明

    NIFI的使用: 注意:FlowFile由[属性]和[内容]组成,在解析的过程中这个概念非常重要,因为有些组件操作的是属性,有些组件操作的是内容,在配置组件时Destination配置项的选择很重要, ...

  5. join()方法作用

    当在主线程当中执行到t1.join()方法时,就认为主线程应该把执行权让给t1 废话不多说看代码: package com.toov5.thread; public class JoinThreadT ...

  6. 如何用命令行删除EasyBCD开机选择项?

    用硬盘安装Ubuntu方法的windows双系统电脑上面,很多人都是用EasyBCD设置的开机启动选择.所以当我们不需要双系统的时候,或者已经删除双系统后,或者安装双系统失败的情况下,发现电脑的开机启 ...

  7. 分享知识-快乐自己:Hibernate对象的三种状态

    图解: 1):瞬时状态(Transient) 对象与session没有关联,数据库中没有对应的数据. 一般new出来的对象便是瞬时对象. 对瞬时对象使用save()方法便使之成为持久对象. 由于数据库 ...

  8. linux应用之nginx的源码安装及配置(centos)

    1.准备工作选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL.Nginx是C写的,需要用GCC编译:Nginx的 ...

  9. python基础-正则2

    正则函数 Python提供re模块,包含所有正则表达式的功能 由于python的字符串本身也有\转义,所以需要注意: s = "ABC\\-001" 对应的正则表达式应为:'ABC ...

  10. PG替换字段中的回车与换行

    REPLACE(filed, CHR(10), '') //替换换行符 REPLACE(filed, CHR(13), '') //替换回车符