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. POI向Excel中写入数据及追加数据

    import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...

  2. JSP不支持EL表达式的解决方案

    EL的全称是Expression Language.1.在默认情况下,Servlet 2.3 / JSP 1.2是不支持EL表达式的,而Servlet 2.4 / JSP 2.0支持. servlet ...

  3. 16/7/7_PHP-Static静态关键字

    Static静态关键字 静态属性与方法可以在不实例化类的情况下调用,直接使用类名::方法名的方式进行调用.静态属性不允许对象使用->操作符调用. class Car { private stat ...

  4. struts2 基础3 文件上传、拦截器

    文件上传: 1.将头设置为enctype=”multipart/form-data” <form action="${pageContext.request.contextPath } ...

  5. [Python3 练习] 001 温度转换1

    题目:温度转换 I (1) 描述 温度的刻画有两个不同体系:摄氏度 (Celsius) 和华氏度 (Fabrenheit) 请编写程序将用户输入的华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度 转换 ...

  6. Java 13 发布了!

    点击上方蓝色链接,关注并"设为星标" Java干货,每天及时推送 通告一下,Java 13 09/17 发布了,尝鲜地址: https://www.oracle.com/techn ...

  7. [2019杭电多校第六场][hdu6641]TDL

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6641 题意为求出最小的n,满足(f(n,m)-n)^n=k,其中f(n,m)为第m大的x,其中x满足g ...

  8. linux驱动模型——platform(1)

    一.驱动模型包含什么? 1.1. 类class 1.1.2. 它能够自动创建/dev下的设备节点,不需要mknod /dev/xxx c x x创建.当然class还有其另外的作用,且自动创建设备节点 ...

  9. Pandas 时间序列处理

    目录 Pandas 时间序列处理 1 Python 的日期和时间处理 1.1 常用模块 1.2 字符串和 datetime 转换 2 Pandas 的时间处理及操作 2.1 创建与基础操作 2.2 时 ...

  10. OtterTune配置记录

    0. 准备两台Ubuntu 18.04的虚拟机,安装mysql(供server-side存储调优数据用)和postgresql(供client-side存储业务数据用,这里以PostgreSQL为例. ...