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. IPv6 首部格式

    <图解TCP/IP> 4.8 IPv6的首部格式 IPv6为了减轻路由器的负担,省略了首部校验和字段.因此路由器不再需要计算校验和,从而提高了包的转发效率. 此外,分片处理所用的识别码成为 ...

  2. 类ThreadGroup

    Java中使用ThreadGroup来表示线程组,它可以对一批线程进行分类管理,Java允许程序直接对线程组进行控制. 默认的情况下,所有的线程都属于主线程组. public final Thread ...

  3. centos 6.5 升级openssh-7.5

    1.环境 2.安装telnet 服务,防止ssh升级之后登陆不上服务器,使用telnet 连接服务器 yum install telnet-server -y chkconfig telnet on ...

  4. 剑指offer--day07

    1.1 题目:反转链表:输入一个链表,反转链表后,输出新链表的表头. 1.2 思路:这道题,我们要做到的是反转链表,我们的思路是将前一个节点与后一个节点断开,然后让后一个节点指向前一个节点,这个过程就 ...

  5. 003--PowerDesigner创建索引与外键

    PowerDesigner创建索引与外键 一.创建索引 双击Table->Columns->创建索引 Step1:双击Table Step2:选择Columns->创建索引 弹出如下 ...

  6. xmake v2.1.9版本发布,增加可视化图形菜单配置

    此版本主要增加xmake f --menu实现用户自定义图形菜单配置,界面风格类似linux的make menuconfig: [图片上传失败-(image-505bc0-1517795319124) ...

  7. [2019杭电多校第二场][hdu6599]I Love Palindrome String(回文自动机&&hash)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 题目大意为求字符串S有多少个子串S[l,r]满足回文串的定义,并且S[l,(l+r)/2]也满足 ...

  8. [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树)

    [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树) 题面 略 分析 首先把一组询问(x,y)看成二维平面上的一个点,我们想办法用数据结构维护这个二维平面(注意根据题意这 ...

  9. Django中orm的惰性机制

    那么首先要知道什么是ORM 专业化的角度来说:叫对象关系映射(Object-Relation Mapping)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 那具体ORM是什么呢?:( ...

  10. Runnable和Thread区别和比较

    在很多博客中用这样一个例子来说明 Runnable更容易实现资源共享,能多个线程同时处理一个资源. 看代码: public static void main(String[] args) { new ...