transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1077    Accepted Submission(s): 521

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
 
 
题意:有n个城市,n-1条道路使其成为一个树,每个城市有一种书以及对应的价格。现在一位商人想要从一个城市买入书,另一个城市卖出,利润为卖出的钱减去买入+路费的花费。问最大利润是多少。
 
思路:我们从一个点出发,遍历每一个点,求出经过(或不经过)在以这个点为根的子树的最优解。
最优解分为两个部分:最小成本,最大卖出价格
 
这个点的最小成本=min(子节点的最小成本+路费, 这个点的买入价),即在“经过这个点”与“以这个点为起点”之间选取一个最优状态
              最大卖出价=max(子树内的最大卖出价-路费, 这个点的卖出价),即在“经过这个点”与“以这个点为终点”之间选取一个最优状态
 
在遍历时更新答案,最后输出即可。
由于遍历时传递最优解,dfs返回的是最优状态
 
AC代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
const int MAXN=1e5+;
typedef pair<int, int> pii;
struct Node{
int to;
int cost;
};
vector<Node> vec[MAXN];
int price[MAXN],res;
bool vis[MAXN];
int min(int a, int b)
{
return (a>b)?b:a;
}
int max(int a, int b)
{
return (a>b)?a:b;
}
pii dfs(int p)
{
vis[p]=;
int minin=price[p],maxout=price[p];
for(int i=;i<vec[p].size();i++){
Node e=vec[p][i];
if(vis[e.to]) continue; pii ans=dfs(e.to);
minin=min(e.cost+ans.second, minin);
maxout=max(ans.first-e.cost, maxout);
}
res=max(res, maxout-minin);
return pii(maxout, minin);
}
int main()
{
int T,n;
scanf("%d", &T);
while(T--)
{
memset(vis, , sizeof(vis));
scanf("%d", &n);
for(int i=;i<=n;i++){
vec[i].clear();
scanf("%d", &price[i]);
}
int a,b,c;
Node N;
for(int i=;i<n-;i++){
scanf("%d %d %d", &a, &b, &c);
vec[a].push_back(Node{b,c});
vec[b].push_back(Node{a,c});
}
res=;
dfs();
printf("%d\n",res);
}
return ;
}

2017ICPC沈阳网络赛 HDU 6201 -- transaction transaction transaction(树上dp)的更多相关文章

  1. 2017ICPC沈阳网络赛 HDU 6205 -- card card card(最大子段和)

    card card card Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. 2019 沈阳网络赛 D Fish eating fruit ( 树形DP)

    题目传送门 题意:求一颗树中所有点对(a,b)的路径长度,路径长度按照模3之后的值进行分类,最后分别求每一类的和 分析:树形DP \(dp[i][j]\) 表示以 i 为根的子树中,所有子节点到 i ...

  3. 2018 ICPC 沈阳网络赛

    2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...

  4. ccpc 网络赛 hdu 6155

    # ccpc 网络赛 hdu 6155(矩阵乘法 + 线段树) 题意: 给出 01 串,要么询问某个区间内不同的 01 子序列数量,要么把区间翻转. 叉姐的题解: 先考虑怎么算 \(s_1, s_2, ...

  5. 沈阳网络赛 F - 上下界网络流

    "Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...

  6. HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...

  7. HDU 6200 2017沈阳网络赛 树上区间更新,求和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6200 题意:给个图,有2种操作,一种是加一条无向边,二是查询u,v之间必须有的边的条数,所谓必须有的边 ...

  8. HDU 6199 2017沈阳网络赛 DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6199 题意:n堆石子,Alice和Bob来做游戏,一个人选择取K堆那么另外一个人就必须取k堆或者k+1 ...

  9. HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意:n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V ...

随机推荐

  1. 百度人脸识别python调用例子

    # 首先pip install baidu-aip # SDK文档链接http://ai.baidu.com/docs#/Face-Python-SDK/top import base64 from ...

  2. webpack构建项目连接本机IP仍无法访问问题

    通过连接IP地址,确定连接成功后仍无法访问本机运行项目,需要对项目配置进行修改,有两种情况: 第一种是在config/index.js,把module.exports={}中找到 host:'loca ...

  3. Linux(Ubuntu)常用命令(三)

    查看时间  cal :显示当前日期.  cal :显示全年日历./ cal -y 显示当年日历.  date :显示当前时间. 这几个一般不会用到了解即可. 查看进程信息  ps :显示当前进程. - ...

  4. 【CTS】几个serialno失败项

    [问题结论] [Common]SN配置项的问题,只可以'数字与大小写字母' 将配置SN改为字母数字组合,测试全部pass [问题描述] CTS三条失败项 run cts -m CtsTelephony ...

  5. LeetCode 算法 Part 1

    目录 1. 两数之和 1. 题目 2.代码 4. 算法用时 5. 感想 2. 两数相加 1. 题目 2.代码 4. 算法用时 5. 感想 3. 无重复字符的最长子串 1. 题目 2.代码 4. 算法用 ...

  6. Node.js实战13:fs模块奥义!开发一个数据库。

    本文,将使用fs开发一种简单的文件型数据库. 数据库中,记录将采用JSON模式,内容型如: {"key":"a","value":" ...

  7. Tomcat控制台

    一般在安装完成Tomcat之后,我们需要验证tomcat是否安装成功,在浏览器的url中输入:http://127.0.0.1:8080/,就会进入如下的页面(表示安装成功): 在上面的左侧顶部,有一 ...

  8. python之getopt

    getopt可以分析输入的参数,根据不同的参数输入不同的命令 getopt.getopt( [命令行参数列表], "短选项", "长选项列表" ) getopt ...

  9. jquery悬停和移出事件

    $('#hides').mouseover(function () { alert("sdfdsf")}).mouseout(function () { alert("啊 ...

  10. 【转载】研发应该懂的binlog知识(下)

    引言 这篇是<研发应该懂的binlog知识(上)>的下半部分.在本文,我会阐述一下binlog的结构,以及如何使用java来解析binlog.不过,话说回来,其实严格意义上来说,研发应该还 ...