2017ICPC沈阳网络赛 HDU 6201 -- transaction transaction transaction(树上dp)

transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1077    Accepted Submission(s): 521


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.
 
Input
The first line contains an integer T (1T10) , the number of test cases. 
For each test case:
first line contains an integer n (2n100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1Price10000) 
then follows n1 lines, each contains three numbers xy and z which means there exists a road between x and y, the distance is zkm (1z1000)
 
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
 
 
题意:有n个城市,n-1条道路使其成为一个树,每个城市有一种书以及对应的价格。现在一位商人想要从一个城市买入书,另一个城市卖出,利润为卖出的钱减去买入+路费的花费。问最大利润是多少。
 
思路:我们从一个点出发,遍历每一个点,求出经过(或不经过)在以这个点为根的子树的最优解。
最优解分为两个部分:最小成本,最大卖出价格
 
这个点的最小成本=min(子节点的最小成本+路费, 这个点的买入价),即在“经过这个点”与“以这个点为起点”之间选取一个最优状态
              最大卖出价=max(子树内的最大卖出价-路费, 这个点的卖出价),即在“经过这个点”与“以这个点为终点”之间选取一个最优状态
 
在遍历时更新答案,最后输出即可。
由于遍历时传递最优解,dfs返回的是最优状态
 
AC代码:
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<vector>
 5 using namespace std;
 6 const int MAXN=1e5+10;
 7 typedef pair<int, int> pii;
 8 struct Node{
 9     int to;
10     int cost;
11 };
12 vector<Node> vec[MAXN];
13 int price[MAXN],res;
14 bool vis[MAXN];
15 int min(int a, int b)
16 {
17     return (a>b)?b:a;
18 }
19 int max(int a, int b)
20 {
21     return (a>b)?a:b;
22 }
23 pii dfs(int p)
24 {
25     vis[p]=1;
26     int minin=price[p],maxout=price[p];
27     for(int i=0;i<vec[p].size();i++){
28         Node e=vec[p][i];
29         if(vis[e.to]) continue;
30         
31         pii ans=dfs(e.to);
32         minin=min(e.cost+ans.second, minin);
33         maxout=max(ans.first-e.cost, maxout);
34     }
35     res=max(res, maxout-minin);
36     return pii(maxout, minin);
37 }
38 int main()
39 {
40     int T,n;
41     scanf("%d", &T);
42     while(T--)
43     {
44         memset(vis, 0, sizeof(vis));
45         scanf("%d", &n);
46         for(int i=1;i<=n;i++){
47             vec[i].clear();
48             scanf("%d", &price[i]);
49         }
50         int a,b,c;
51         Node N;
52         for(int i=0;i<n-1;i++){
53             scanf("%d %d %d", &a, &b, &c);
54             vec[a].push_back(Node{b,c});
55             vec[b].push_back(Node{a,c});
56         }
57         res=0;
58         dfs(1);
59         printf("%d
",res);
60     }
61     return 0;
62 } 
原文地址:https://www.cnblogs.com/MasterSpark/p/7510488.html