HDU沈阳网络赛:transaction transaction transaction(树形dp & 最短路)
来源:互联网 发布:冷核聚变知乎 编辑:程序博客网 时间:2024/05/21 11:23
transaction transaction transaction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 2245 Accepted Submission(s): 682
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 isai yuan in i t city. Kelukin will take taxi, whose price is 1 yuan per km and this fare cannot be ignored.
There aren−1 roads connecting n cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.
As we know, the price of this book was different in each city. It is
There are
Input
The first line contains an integer T (1≤T≤10 ) , the number of test cases.
For each test case:
first line contains an integern (2≤n≤100000 ) means the number of cities;
second line containsn numbers, the i th number means the prices in i th city; (1≤Price≤10000)
then followsn−1 lines, each contains three numbers x , y and z which means there exists a road between x and y , the distance is z km (1≤z≤1000) .
For each test case:
first line contains an integer
second line contains
then follows
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 301 3 23 4 10
Sample Output
8
①树形dp:
dp[i][0]为选择以i为根的子树作为卖出点获得得最大值
dp[i][1]为选择以i为根的子树作为买入点获得得最大值
因为要处理同一根的两颗子树的情况,
所以进入节点时初始化为dp[cur][1] = a[cur],dp[cur][0] = -a[cur];
转移:dp[cur][1] = max(dp[to][1]-w, dp[cur][1])
dp[cur][0] = max(dp[to][0]-w, dp[cur][0]);
维护一个最大收益
ans = max(ans, max(dp[to][1]+dp[cur][0]-w, dp[to][0]+dp[cur][1]-w));
# include <bits/stdc++.h># define pb push_back# define mp make_pair# define A first# define B secondusing namespace std;const int maxn = 1e5+30;vector<pair<int,int> >g[maxn];int dp[maxn][2], a[maxn], ans;void dfs(int cur, int pre){ dp[cur][1] = a[cur]; dp[cur][0] = -a[cur]; for(auto j : g[cur]) { int to = j.A, w = j.B; if(to == pre) continue; dfs(to, cur); ans = max(ans, max(dp[to][1]+dp[cur][0]-w, dp[to][0]+dp[cur][1]-w)); dp[cur][1] = max(dp[to][1]-w, dp[cur][1]); dp[cur][0] = max(dp[to][0]-w, dp[cur][0]); }}int main(){ int T, n, u, v, w; scanf("%d",&T); while(T--) { ans = 0; scanf("%d",&n); for(int i=1; i<=n; ++i) g[i].clear(), scanf("%d",&a[i]); for(int i=1; i<n; ++i) { scanf("%d%d%d",&u,&v,&w); g[u].pb(mp(v,w)); g[v].pb(mp(u,w)); } dfs(1, 0); printf("%d\n",ans); } return 0;}
②最短路
比赛时队友大大先想出了这个做法,每个节点复制3份,中间那份双向边,建超级源点和超级终点,类似网络流那样建图,跑一遍最短路即可。
代码略。
阅读全文
0 0
- HDU沈阳网络赛:transaction transaction transaction(树形dp & 最短路)
- 2017沈阳网络赛 1008 HDU 6201 transaction transaction transaction(树形dp)
- ICPC2017网络赛(沈阳)1008 transaction transaction transaction——树形DP
- HDU 6201 transaction transaction transaction(网络流+最短路)
- hdu6201 transaction transaction transaction 树形dp || 最短路
- 【2017沈阳网络赛】1008 hdu6201 transaction transaction transaction 树形dp
- hdu2017沈阳网络赛(2)transaction(从最短路到树dp)
- HDU 6201 transaction transaction transaction &&沈阳网络赛1008
- HDU 6201 transaction transaction transaction (2017沈阳网络赛
- HDU 6201 transaction transaction transaction (2017沈阳网络赛
- HDU-6201 transaction transaction transaction(树形dp)
- hdu 6201 transaction transaction transaction 树形dp
- HDU 6201 transaction transaction transaction(最短路)
- HDU 6201 transaction transaction transaction (树形DP or 拆点最短路)
- HDU 6201 transaction transaction transaction(树形dp)
- HDU 6201 transaction transaction transaction (树形dp)
- HDU 6201 transaction transaction transaction【树形DP||SPFA最长路】
- 【最短路 spfa && 水题】hdu-6201 transaction transaction transaction
- react-native imageButton 可设置默认图片,避免加载失败空白
- Java]NIO:使用Channel、Charset(字符集)、使用Charset传递CharBuffer
- Excel设置扩展数据区域实现输入新行自动扩展上行公式
- Collections
- C++ bug tips
- HDU沈阳网络赛:transaction transaction transaction(树形dp & 最短路)
- 第二周项目1-多文件程序的组织(课程主页版)
- Ubuntu16.04安装inetsim模拟Internet
- Redis与Memcached区别
- mysql having的用法
- 有史以来在表格下方新增空行最快捷的方法(没有之一)
- Appach_Tomcat整合架设
- C++读写HDFS代码
- oracle创建数据库以及用户表空间