NOIP2009 Best Trade

Topic description

C has n large cities and m roads, and each road connects two of the n cities. At most one road is directly connected between any two cities. Some of the m roads are one-way traffic roads, and some are two-way traffic roads, and two-way traffic roads are also counted as one road when counting the number of roads.

Country C has a vast territory, and the distribution of resources varies from place to place, which leads to the fact that the price of the same commodity in different cities is not necessarily the same. However, the buying and selling prices of the same commodity in the same city are always the same.

Businessman Aaron came to C country for tourism. When he learned that the price of the same product may be different in different cities, he decided to use the difference in the price of the product in different cities to earn a little travel expenses while traveling. Assuming that the labels of n cities in country C are from 1 to n, Aaron decides to start from city 1, and finally ends his trip in city n. In the process of traveling, any city can be repeatedly passed through many times, but it is not required to pass through all n cities. Aaron earns travel expenses through such trade: he will choose a city he passes through to buy his favorite commodity - a crystal ball, and sell the crystal ball in another city he passes through later, using the difference earned as travel expenses. Since Aaron mainly travels to country C, he decides that this trade can only be carried out once at most. Of course, he does not need to trade if he cannot earn the price difference.

Assume that C has 5 big cities, the city number and road connection are as shown in the figure below, the one-way arrow indicates that the road is one-way traffic, and the two-way arrow indicates that the road is two-way traffic.

Suppose the crystal ball prices in cities 1~n are 4, 3, 5, 6, and 1, respectively.

Aaron can choose one of the following routes: 1->2->3->5, and buy the crystal ball at the price of 3 in city 2 and sell the crystal ball at the price of 5 in city 3, earning travel expenses The number is 2.

Aaron can also choose the following route 1->4->5->4->5, and buy the crystal ball at the price of 1 when he arrives at city 5 for the first time, and when he arrives at city 4 for the second time Sell ​​the crystal ball for 6 and earn 5 travel.

Now given the crystal ball price of n cities, the information of m roads (the number of the two cities connected by each road and the traffic conditions of the road). Please tell Aaron the maximum he can earn for travel.

Input and output format

Input format:

The first line contains two positive integers n and m, separated by a space, representing the number of cities and the number of roads, respectively.

The second line contains n positive integers, separated by a space between each two integers, which respectively represent the commodity prices of these n cities in the order of labels.

Next m lines, each line has 3 positive integers, x, y, z, separated by a space between each two integers. If z=1, it means that the road is a one-way road between city x and city y; if z=2, it means that this road is a two-way road between city x and city y.

Output format:

The output file trade.out has 1 line and contains 1 integer representing the maximum travel cost that can be earned. If there is no trade, 0 is output.

Input and output example

Input Example #1: Copy
5 5
4 3 5 6 1
1 2 1
1 4 1
2 3 2
3 5 1
4 5 2
Output Sample #1: Copy
5

illustrate

【data range】

The input data guarantees that city 1 can reach city n.

For 10% of the data, 1≤n≤6.

For 30% of the data, 1≤n≤100.

For 50% of the data, there is no tourist route that can start from a city and return to that city.

For 100% data, 1≤n≤100000, 1≤m≤500000, 1≤x, y≤n, 1≤z≤2, 1≤each city

Crystal ball price≤100.

The third question of NOIP 2009 improvement group

 

Refer to dalao's tree dp practice

 

 1 //2018年4月30日12:20:22
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int N = 100001;
 8 const int M = 500001; 
 9 
10 const int INF = 1e9+7; 
11 
12 int n, m;
13 int w[N];
14 
15 int fir[N], nxt[M], to[M], edge_num;
16 void addEdge(int x, int y){
17     to[++edge_num] = y;
18     nxt[edge_num] = fir[x];
19     fir[x] = edge_num;
20 }
21 
22 int f[N], mi[N];
23 
24 void dfs(int x, int minx, int pre){
25     int flag = 1;
26     minx = min(w[x], minx);
27     if(mi[x] > minx) mi[x] = minx, flag = 0;
28     int maxx = max(f[pre], w[x]-minx);
29     if(f[x] < maxx) f[x] = maxx, flag = 0;
30     if(flag) return;
31     for(int i=fir[x]; i; i=nxt[i]) dfs(to[i], minx, x);
32 }
33 
34 int main(){
35     scanf("%d%d", &n, &m); 
36     for(int i=1; i<=n; i++)
37         scanf("%d", &w[i]);
38     for(int i=1; i<=N; i++)
39         mi[i] = INF;
40     for(int i=1; i<=m; i++){
41         int x, y, z;
42         scanf("%d%d%d", &x, &y, &z);
43         if(z == 1){
44             addEdge(x, y); 
45         }else if(z == 2){
46             addEdge(x, y); addEdge(y, x);
47         }
48     }
49     
50     dfs(1, INF, 0);
51     
52     printf("%d\n", f[n]);
53 
54     return 0;
55 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325100076&siteId=291194637