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. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_9_JDK7和JDK9流中异常的处理

    jdk7 jdk9

  2. node+express 中安装nodemon实时更新server.js

    每次启动node server.js,有一个缺点,每次server.js文件有改动时,必须重新执行指令node server.js,新的代码才会起作用 解决方案1 全局安装 npm install s ...

  3. Python笔记(二十八)_魔法方法_迭代器

    迭代器用于遍历容器中的数据,但它不是容器,它是一个实现了__next__方法的对象 与迭代器相关的内置函数: iter(): 将一个对象转换成一个迭代器 next(): 访问迭代器中的下一个变量,直到 ...

  4. JavaScript求两点之间相对于Y轴的顺时针旋转角度

    需求: 已知一个向量,初始位置在y轴方向,如图红色箭头,绕中心点(x1, y1)旋转若干角度后,到达Line(x2,y2 x1,y1)的位置,求旋转角度 分析: 坐标点(x1, y1)(x2, y2) ...

  5. Node.js实战12:fs模块高级技巧。

    通过fs模块使用流 fs模块同样有流接口,如下例: var fs = require("fs"); var read_able = fs.createReadStream(&quo ...

  6. Leveldb源码分析--2

    coming from http://blog.csdn.net/sparkliang/article/details/8573618

  7. 如何选择适合自己的Linux版本

    如何选择适合自己的Linux版本: 1.Linux桌面系统,首选Ubuntu; 2.服务器端的Linux系统,首选RHEL或CentOS,这两种中首选CentOS,如果公司有钱,不在乎成本也可以选择R ...

  8. PHP的设计模式及场景应用介绍

    有大量的文章解释什么是设计模式,如何实现设计模式,网络上不需要再写一篇这样的文章.相反,在本文中我们更多的讨论什么时候用和为什么要用,而不是用哪一个和如何使用. 我将会为这些设计模式描绘不同的场景和案 ...

  9. WOJ#3836 Sightseeing Trip

    描述 给定一张无向图,求图中一个至少包含 3 个点的环,环上的节点不重复,并且环上的边的长度之和最小.该问题称为无向图的最小环问题.在本题中,你需要输出最小环的方案,若最小环不唯一,输出任意一个均可. ...

  10. 10、应用机器学习的建议(Advice for Applying Machine Learning)

    10.1 决定下一步做什么 到目前为止,我们已经介绍了许多不同的学习算法,如果你一直跟着这些视频的进度学习,你会发现自己已经不知不觉地成为一个了解许多先进机器学习技术的专家了. 然而,在懂机器学习的人 ...