题意:给定一棵树,每个点有一个点权,每条边也是,找一条路径,问你 T-S-sum,T表示路径的终点的权值,S表示路径始点的权值,sum表示从S到T的边权和。

析:把这一条路径拆开来看,那么就是必然是从 a 先经过一个公共祖先 i,然后再到达b,所以,dp[i][0] 表示 从 i 结点到子树结点中能得到的最大值(到终点),dp[i][1] 表示从子结点到 i 结点能得到的最大值(始点),然后答案就是 dp[i][0] + dp[i][1]。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 50;
const int mod = 1000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r > 0 && r <= n && c > 0 && c <= m;
} struct Edge{
int to, next, c;
};
Edge edge[maxn<<1];
int head[maxn], cnt;
int a[maxn]; void addEdge(int u, int v,int c){
edge[cnt].to = v;
edge[cnt].c = c;
edge[cnt].next = head[u];
head[u] = cnt++;
} int dp[maxn][2];
int ans; void dfs(int u, int fa){
dp[u][0] = a[u];
dp[u][1] = -a[u];
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v == fa) continue;
dfs(v, u);
dp[u][0] = max(dp[u][0], dp[v][0] - edge[i].c);
dp[u][1] = max(dp[u][1], dp[v][1] - edge[i].c);
}
ans = max(ans, dp[u][0] + dp[u][1]);
} int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
ms(head, -1); cnt = 0;
for(int i = 1; i < n; ++i){
int u, v, c;
scanf("%d %d %d", &u, &v, &c);
addEdge(u, v, c);
addEdge(v, u, c);
}
ans = 0;
dfs(1, -1);
printf("%d\n", ans);
}
return 0;
}

  

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. 第十章 hbase默认配置说明

    hbase.rootdir:这个目录是region  server的共享目录,用来持久化Hbase.URL需要是'完全正确'的,还要包含文件系统的scheme.例如,要表示hdfs中的 '/hbase ...

  2. soap,socket

    Socket 接口是访问 Internet 使用得最广泛的方法. 如果你有一台刚配好TCP/IP协议的主机,其IP地址是202.120.127.201, 此时在另一台主机或同一台主机上执行ftp 20 ...

  3. (转)python virtual_env 的使用 + 将原来的虚拟环境部署到新环境

    原文链接: https://blog.csdn.net/poxiaonie/article/details/78820015

  4. 使用Ajax解析数据遇到的问题

    数据格式 我最近在使用JQuery的$.ajax访问后台的时候,发现竟然无法解析返回的数据,具体的错误记不清了(以后在遇到问题先截个图),可以在浏览器的Console中看到一个错误,但是去看这条请求是 ...

  5. std::thread函数传参拷贝次数

    c++11的thread库大大方便了开发,但是目前网络上少有深入分析的资料和使用例程.特别是在线程函数传参这一块,一般止步于使用std::ref传引用. 这次写服务器遇到个BUG,线程函数参数是智能指 ...

  6. 这样学Linux基本命令,事半功倍

    命令基本格式 (1)命令提示符 如下是命令行的命令提示符,以此为例,讲解含义. 其中: root 当前登录用户名 localhost 主机名 ~ 当前所在的目录(即家目录,用户登录的初始位置) # 超 ...

  7. PHP如何知道一个类中所有的方法

    当我们使用一个类时既没有源码也没有文档时(尤其是php扩展提供的类,比如mysqli,Redis类),我们该怎么知道这个类中提供了哪些方法,以及每个方法该怎么使用呢,此时就该PHP中强大的反射登场了, ...

  8. JQueryDOM节点操作

    你一.JQueryDom节点操作 2.1查找节点 获取p节点 var $pDm=$("p"); 输出p节点的title属性 alert($pDm.attr("title& ...

  9. 03-24 Winform图表Chart

    图表主要分为以下几个部分: 1.Annotations--图形注解集合, 2.ChartAreas--图表区域集合, 3.Legends--图例集合, 4.Series--图表序列集合(即图表数据对象 ...

  10. Python基础学习九 单元测试

    import unittest import HTMLTestRunner #产生测试报告 from BeautifulReport import BeautifulReport def calc(x ...