Luogu P1073 optimal trade, luogup1073
Description
C Has n major cities and m roads, each of which connects one or two of the n cities. A maximum of one road can be directly connected between any two cities. One part of the m-way road is a one-way road, and one part is a two-way road.
Country C has a vast territory and different regions have different resource distribution, which leads to different prices for the same product in different cities. However, the prices and prices of the same product in the same city are always the same.
Merchant Aaron traveled to country C. After learning that the prices of the same product in different cities may be different, he decided to use the price difference of the product in different cities to make a little travel expenses while traveling. The number of n cities in country C ranges from 1 ~ N. Aaron decided to start from City 1 and end his trip in city n. During the tourism process, any city can go through multiple times, but it is not required to go through all n cities. A dragon earned travel expenses through such a trade method: he will choose a city that passes by to buy his favorite goods-crystal ball, and sell the crystal ball in another city that passes, the difference is used as travel expenses. As Aaron traveled mainly to country C, he decided that the trade would only be conducted at most once. Of course, he did not need to trade if he could not make any difference.
Assume that there are five major cities in China. The city numbers and road connections are shown in the following figure. One-way arrows indicate one-way roads and two-way arrows indicate two-way roads.
Suppose 1 ~ The crystal ball prices in city n are 4, 3, 5, 6, and 1, respectively.
A dragon can choose the following line: 1-> 2-> 3-> 5, and buy a crystal ball at the price of 3 in City 2, in city 3, the crystal ball is sold at the price of 5, and the travel cost is 2.
You can also choose the next line, such as 1-> 4-> 5-> 4-> 5, and buy a crystal ball at the price of 1 when you reach city 5 1st times, when 2nd times reaches City 4, the crystal ball is sold at the price of 6, earning 5 travel expenses.
Now we provide the crystal ball price for n cities and m road information (numbers of the two cities connected by each road and the traffic conditions of the road ). Please tell Aaron how much travel he can earn.
Input/Output Format
Input Format:
The first line contains two positive integers n and m, separated by a space in the middle, indicating the number of cities and the number of roads respectively.
There are n positive integers in the second line. Each two integers are separated by a space and the prices of goods in the n cities are displayed in the order of numbers.
In the next m row, each row has three positive integers, x, y, and z, which are separated by a space. If z = 1, it indicates that this road is a one-way road between city x and city y. If z = 2, it indicates that this road is a two-way road between city x and city y.
Output Format:
The output file trade. out contains one row in total and contains an integer, indicating the maximum amount of travel expenses that can be earned. If there is no trade, 0 is output.
Input and Output sample input sample #1:
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:
5
Description
[Data Scope]
Enter data to ensure that city 1 can reach city n.
For 10% of the data, 1 ≤ n ≤ 6.
For 30% of data, 1 ≤ n ≤ 100.
There is no travel route for 50% of the Data. You can start from a city and return to the city.
For 100% of data, 1 ≤ n ≤ 100000,1 ≤ m ≤ 500000,1 ≤ x, y ≤ n, 1 ≤ z ≤ 2 ≤ 1 ≤ cities
Crystal Ball price ≤ 100.
NOIP 2009 raise Group's third question
In doing this, I made many small mistakes. I made a spfa and wrote the vis array as dis, but I never found it, but the magic is that this is an example, then crazy WA ......
The idea for this question is simple:
Any point on the road from the start point to the end point, buy in the closed interval before the start point, and throw out in the pre-closed Opening Zone. The maximum price difference is the best trade.
Naturally, I thought of spfa, but spfa relies on edge weight for calculation. What should I do? We can assign the vertex right to the adjacent edge right.
Many details need to be dealt with in this question:
1. creating images is complicated and it is necessary to check clearly.
2. This figure has a ring! The condition will get spfa stuck!
3. for each operation (each edge), the value of the edge should be 1. The Edge Weight 2. The start value 3. The value of the end is filtered by min.
4. Initialization is very important, otherwise it will lead to a dead end!
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int gg=1000000+5; 4 int n,m; 5 int b[100000+2]; 6 int dis[gg],dis2[gg]; 7 struct node{ 8 int w; 9 int to; 10 int net; 11 }a[gg],aa[gg]; 12 bool vis[gg],vis2[gg]; 13 int head[gg],head2[gg]; 14 int cnt,cnt2; 15 16 inline void add2(int i,int j,int w) 17 { 18 aa[++cnt2].to=j; 19 aa[cnt2].net=head2[i]; 20 aa[cnt2].w=w; 21 head2[i]=cnt2; 22 } 23 24 inline void add(int i,int j,int w) 25 { 26 a[++cnt].to=j; 27 a[cnt].net=head[i]; 28 a[cnt].w=w; 29 head[i]=cnt; 30 } 31 32 inline void spfa(int s) 33 { 34 deque<int>q; 35 for(int i=1;i<=n;i++) 36 dis[i]=999999; 37 memset(vis,false,sizeof(vis)); 38 q.push_back(s); 39 dis[s]=0; 40 vis[s]=true; 41 while(!q.empty()) 42 { 43 int u=q.front(); 44 q.pop_front(); 45 vis[u]=false; 46 for(int i=head[u];i;i=a[i].net) 47 { 48 int v=a[i].to; 49 if(dis[v]>min(dis[u],a[i].w)) 50 { 51 dis[v]=min(dis[u],a[i].w); 52 if(!vis[v]) 53 { 54 vis[v]=true; 55 if(q.empty()||dis[v]>dis[q.front()]) 56 { 57 q.push_back(v); 58 } 59 else 60 { 61 q.push_front(v); 62 } 63 } 64 } 65 } 66 } 67 } 68 69 inline void spfa2(int s) 70 { 71 deque<int>q; 72 for(int i=1;i<=n;i++) 73 dis2[i]=0; 74 memset(vis2,false,sizeof(vis2)); 75 q.push_back(s); 76 dis2[s]=0; 77 vis2[s]=true; 78 while(!q.empty()) 79 { 80 int u=q.front(); 81 q.pop_front(); 82 vis2[u]=false; 83 for(int i=head2[u];i;i=aa[i].net) 84 { 85 int v=aa[i].to; 86 if(dis2[v]<max(dis2[u],aa[i].w)) 87 { 88 dis2[v]=max(dis2[u],aa[i].w); 89 if(!vis2[v]) 90 { 91 vis2[v]=true; 92 if(q.empty()||dis2[v]>dis2[q.front()]) 93 { 94 q.push_back(v); 95 } 96 else 97 { 98 q.push_front(v); 99 }100 }101 }102 }103 }104 }105 106 int ans=0;107 108 int main()109 {110 cin>>n>>m;111 for(int i=1;i<=n;i++)112 {113 cin>>b[i];114 }115 for(int i=1;i<=m;i++)116 {117 int q,w,e;118 cin>>q>>w>>e;119 if(e==1)120 {121 add(q,w,b[w]);122 add2(w,q,b[q]);123 }124 else if(e==2)125 {126 add(q,w,b[w]);127 add(w,q,b[q]);128 add2(q,w,b[w]);129 add2(w,q,b[q]);130 }131 }132 spfa(1);133 spfa2(n);134 for(int i=1;i<=n;i++)135 {136 ans=max(ans,dis2[i]-dis[i]);137 }138 cout<<ans<<endl;139 return 0;140 }