transaction transaction transaction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 1256 Accepted Submission(s): 601
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 i t city. Kelukin will take taxi, whose price is 1 yuan 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 i t city. Kelukin will take taxi, whose price is 1 yuan 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 i th number means the prices in i th 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 z km (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 i th number means the prices in i th 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 z km (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
Source
Recommend
题意:给你一颗树,边有权值,现在有本书,每个点都有这本书的价格,现在你可以选任意一点作为起点买这本书,跑去任意一点卖掉,过程中消耗路径的权值花费,问你任选起点和终点最大收益是多少。
思路:树形DP,dp[u][0]表示以u为根的子树中买一本书的最大收益,dp[u][1]表示以u为根的子树中卖一本书的最大收
益。dp[u][0]+dp[u][1]即为在以u为根的子树中选两点的最大收益。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 5;
struct node
{
int to, w;
node(){}
node(int tt, int ww) : to(tt), w(ww){}
};
vector<node> v[maxn];
int n, dp[maxn][2], val[maxn], ans;
void dfs(int u, int f)
{
dp[u][0] = val[u];
dp[u][1] = -val[u];
for(int i = 0; i < v[u].size(); i++)
{
int to = v[u][i].to;
int w = v[u][i].w;
if(to == f) continue;
dfs(to, u);
dp[u][0] = max(dp[u][0], dp[to][0]-w);
dp[u][1] = max(dp[u][1], dp[to][1]-w);
}
ans = max(ans, dp[u][1]+dp[u][0]);
}
int main()
{
int x, y, z, _;
cin >> _;
while(_--)
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &val[i]);
for(int i = 1; i <= n; i++)
v[i].clear();
for(int i = 1; i <= n-1; i++)
{
scanf("%d%d%d", &x, &y, &z);
v[x].push_back(node(y, z));
v[y].push_back(node(x, z));
}
ans = 0;
dfs(1, -1);
printf("%d\n", ans);
}
return 0;
}
拆点加最长路
看到这题就是求未知起点的最长路,但是它有边权,也有点权啊怎么办,可以把点拆成入点和出点,然后构造源点S和终点T,然后这样连边(由于我用最长路求,显然记买入和路费为负,卖出为正):
,自身拆点肯定要连;
、
,由于要求价值最大,花费显然要负权;
,由于未知起点,那么直接把点都连到S上从S开始,并且这样刚好可以把点权转换成边权
,卖掉第i个后到T点。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int INF = 1e9;
const int maxn = 2e5 + 5;
int dis[maxn], val[maxn], book[maxn], s, t, n;
struct node
{
int val, to;
node(){}
node(int tt, int vv) : to(tt), val(vv){}
};
vector<node> v[maxn];
void init()
{
for(int i = 0; i < maxn; i++)
v[i].clear();
s = 0, t = n*2+1;
}
int spfa(int u)
{
memset(book, 0, sizeof(book));
for(int i = 0; i < maxn; i++) dis[i] = -INF;
dis[u] = 0;
queue<int> q;
q.push(u);
book[u] = 1;
while(!q.empty())
{
u = q.front();
q.pop();
book[u] = 0;
for(int i = 0; i < v[u].size(); i++)
{
int to = v[u][i].to, w = v[u][i].val;
if(dis[to] < dis[u] + w)
{
dis[to] = dis[u] + w;
if(!book[to])
{
book[to] = 1;
q.push(to);
}
}
}
}
return dis[t];
}
int main()
{
int _, x, y, z;
cin >> _;
while(_--)
{
scanf("%d", &n);
init();
for(int i = 1; i <= n; i++)
{
scanf("%d", &val[i]);
v[i].push_back(node(i+n, 0));
v[s].push_back(node(i, -val[i]));
v[i+n].push_back(node(t, val[i]));
}
for(int i = 1; i <= n-1; i++)
{
scanf("%d%d%d", &x, &y, &z);
v[x].push_back(node(y, -z));
v[y].push_back(node(x, -z));
}
printf("%d\n", spfa(s));
}
return 0;
}