2017 ACM/ICPC Asia Regional Shenyang Online transaction transaction transaction

时间:2023-03-08 18:52:04
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.
2017 ACM/ICPC Asia Regional Shenyang Online transaction transaction transaction

题意:+1s 我们把书从这边买,去那边买,还有路费,问最多能赚多少

解法:

dfs 对于每个相邻的点我们做 买点和卖点 减去路费

最后求最大的利润

 #include<bits/stdc++.h>
using namespace std;
int dis[];
int ans;
int dis1[],dis2[];
vector<pair<int,int>>Ve[];
void dfs(int u,int v){
dis1[u]=-*dis[u];
dis2[u]=dis[u];
for(int i=;i<Ve[u].size();i++){
int x=Ve[u][i].first;
int y=Ve[u][i].second;
if(x==v){
continue;
}
dfs(x,u);
dis1[u]=max(dis1[x]-y,dis1[u]);
dis2[u]=max(dis2[x]-y,dis2[u]);
}
ans=max(ans,dis[u]+dis1[u]);
ans=max(ans,dis2[u]-dis[u]);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
ans=;
scanf("%d",&n);
memset(dis1,,sizeof(dis1));
memset(dis2,,sizeof(dis2));
for(int i=;i<=n;i++){
Ve[i].clear();
}
for(int i=;i<=n;i++){
scanf("%d",&dis[i]);
}
for(int i=;i<n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
Ve[x].push_back({y,z});
Ve[y].push_back({x,z});
}
dfs(,);
printf("%d\n",ans);
}
return ;
}