2017 ACM/ICPC Asia Regional Shenyang Online transaction transaction transaction

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 n1 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 对于每个相邻的点我们做 买点和卖点 减去路费

最后求最大的利润

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int dis[200000];
 4 int ans;
 5 int dis1[200000],dis2[200000];
 6 vector<pair<int,int>>Ve[200000];
 7 void dfs(int u,int v){
 8     dis1[u]=-1*dis[u];
 9     dis2[u]=dis[u];
10     for(int i=0;i<Ve[u].size();i++){
11         int x=Ve[u][i].first;
12         int y=Ve[u][i].second;
13         if(x==v){
14             continue;
15         }
16         dfs(x,u);
17         dis1[u]=max(dis1[x]-y,dis1[u]);
18         dis2[u]=max(dis2[x]-y,dis2[u]);
19     }
20     ans=max(ans,dis[u]+dis1[u]);
21     ans=max(ans,dis2[u]-dis[u]);
22 }
23 int main(){
24     int t;
25     scanf("%d",&t);
26     while(t--){
27         int n;
28         ans=0;
29         scanf("%d",&n);
30         memset(dis1,0,sizeof(dis1));
31         memset(dis2,0,sizeof(dis2));
32         for(int i=0;i<=n;i++){
33             Ve[i].clear();
34         }
35         for(int i=1;i<=n;i++){
36             scanf("%d",&dis[i]);
37         }
38         for(int i=1;i<n;i++){
39             int x,y,z;
40             scanf("%d%d%d",&x,&y,&z);
41             Ve[x].push_back({y,z});
42             Ve[y].push_back({x,z});
43         }
44         dfs(1,0);
45         printf("%d\n",ans);
46     }
47     return 0;
48 }

 

转载于:https://www.cnblogs.com/yinghualuowu/p/7550573.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值