您的位置:首页 > 其它

2017 ACM/ICPC Asia Regional Shenyang Online transaction transaction transaction

2017-09-19 10:44 573 查看
[align=left]Problem Description[/align]
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.



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

解法:

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

最后求最大的利润

#include<bits/stdc++.h>
using namespace std;
int dis[200000];
int ans;
int dis1[200000],dis2[200000];
vector<pair<int,int>>Ve[200000];
void dfs(int u,int v){
dis1[u]=-1*dis[u];
dis2[u]=dis[u];
for(int i=0;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=0;
scanf("%d",&n);
memset(dis1,0,sizeof(dis1));
memset(dis2,0,sizeof(dis2));
for(int i=0;i<=n;i++){
Ve[i].clear();
}
for(int i=1;i<=n;i++){
scanf("%d",&dis[i]);
}
for(int i=1;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(1,0);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐