HDU - 6201 transaction transaction transaction(树形dp取两点)
transaction transaction transaction
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取两点)的更多相关文章
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- hdu 4003 Find Metal Mineral 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...
- HDU 5758 Explorer Bo(树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5758 [题目大意] 给出一棵树,每条路长度为1,允许从一个节点传送到任意一个节点,现在要求在传送次 ...
- 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 ...
- HDU 4123 Bob’s Race 树形dp+单调队列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...
- HDU 4799 LIKE vs CANDLE 树形dp
题意:有n个人,他们的关系,形成一棵有根树(0是树根,代表管理员),每个人有一个价值 现在有一条微博,每个人要么点赞,要么送一个蜡烛 初始一些人利用bug反转了某些人的操作(赞变蜡烛 或者 蜡烛变成赞 ...
- hdu 3586 Information Disturbing(树形dp + 二分)
本文出自 http://blog.csdn.net/shuangde800 题目链接: hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...
随机推荐
- java array
1 array变量 Type[] array_virable_name; 2 array对象 2.1 new Type[] array_virable_name = new Type[NUM]; 2. ...
- detached HEAD state
1 detached HEAD state指的是什么 正常情况下,HEAD指向一个branch,而branch又指向一个commit. detached HEAD state指的是HEAD指针没有指向 ...
- phpPHP创建创建jpg格式图片以及压缩图片(转)
其实是因为一些业务上的需求,所以需要对用户上传后的图片进行压缩,因为上传的图片比较大,显示的时候加载起来如果网速不给力的话就很吃力了,而且大图片也浪费空间,于是找了一下相关的资源,主要方法在开源中国上 ...
- 转 Spring 组件 <context:component-scan base-pakage="">用法
1.如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入. <!-- 注解注入 --> <co ...
- CUDA: 共享内存与同步
CUDA C支持共享内存, 将CUDA C关键字__shared__添加到变量声明中,将使这个变量驻留在共享内存中.对在GPU上启动的每个线程块,CUDA C编译器都将创建该变量的一个副本.线程块中的 ...
- linux 下 cat
[root@localhost Data]# cat << end > iptable> 3> 4> 5> > 6> end[root@local ...
- yhdsir@function:php
curl 获取页面信息 function curl_get_content($url){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $u ...
- linux 常用shell脚本语句
最近老大让写一个shell脚本,每天从一个固定IP中取到相应文件,所以想写一个简单的shell脚本命令,供大家学习交流.先做一个简单的例子,先看效果吧, 代码如下: #!/bin/sh #定义一个变量 ...
- cygwin添加到有右键菜单
cygwin添加到有右键菜单 前提 为了在windows中使用cygwin编译指定文件代码更为方便,所以动心思琢磨把cygwin添加到右键菜单,百度了一下,发现很多这样的教程,但是有问题,比如添加了但 ...
- ASP.NET 4.0 页面 ValidateRequest="false" 失效不起作用
当ASP.NET 2.0升级到 ASP.NET 4.0后,页面的 ValidateRequest="false" 不起作用. 因为 ASP.NET 4.0 请求验证被提前到IHtt ...