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 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.
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).
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
①树形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 second
using 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份,中间那份双向边,建超级源点和超级终点,类似网络流那样建图,跑一遍最短路即可。
代码略。