[NOIP 2014 review] Chapter 5: Graph Theory, noip2014

Source: Internet
Author: User

[NOIP 2014 review] Chapter 5: Graph Theory, noip2014
I. Most short-circuit problem 1. Graph storage method 2. Floyd algorithm for multi-source shortest 3. Dijsktra Algorithm for single-source shortest 4. Bellman-Ford Algorithm for single-source shortest 5 SPFA single-source shortest (1) wikioi 1173 optimal trade

DescriptionDescription

[Problem description]
C Has n major cities and m roads, each of which connects one or two of the n cities. Any two
Only one road can be directly connected between cities. Some of these m roads are unidirectional roads, and some are
It is a two-way road, and the number of two-way roads is counted as one.
Country C has a vast territory with different resource distribution, which leads to the price of the same commodity in different cities.
Cells are not necessarily the same. However, the prices and prices of the same product in the same city are always the same.
Merchant Aaron traveled to country C. When he learns that the price of the same product in different cities may be different
Then, we decided to use the price difference of commodities in different cities to earn a little travel expenses while traveling. Set up n cities in country C
The city number ranges from 1 ~ N. Aaron decided to start from City 1 and end his trip in city n. In
During the process, any city can go through multiple times, but it is not required to go through all n cities. Through such a trade party
Way to earn travel expenses: he will choose a city to buy his favorite goods-crystal ball, and
A city sells the crystal ball and uses the price difference as travel expenses. As Aaron traveled mostly to country C, he decided
This trade can only be conducted at most once. Of course, he does not need to trade if he cannot make any difference.
Assume that C has five big cities, and the city numbers and road connections are shown in the following figure. One-way arrows indicate the road.
Is a one-way traffic, two-way arrows indicate that this road is a two-way traffic.

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.
City No. Sold crystal balls at a price of 5, earning 2 yuan in travel expenses.
You can also choose the next line, such as 1-> 4-> 5-> 4-> 5, and set the price to 1 when the first line reaches city 5.
Buy a crystal ball. When you reach City 4 for 2nd times, sell the crystal ball at the price of 6 and earn 5 travel expenses.

Now we provide the crystal ball price for n cities, the information of m roads (numbers of the two cities connected by each Road)
And the traffic of the road ). Please tell Aaron how much travel he can earn.

Input description Input Description

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
Number.
There are n positive integers in the second line. Each two integers are separated by a space and represent the n cities in the order of numbers.
City price.
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 one-way road between city x and city y.
Y.

Output description Output Description

Contains an integer indicating the maximum amount of travel expenses that can be earned. If there is no trade,
The output value is 0.

Sample Input Sample Input

5 5
4 3 5 6 1
1 2 1
1 4 1
2 3 2
3 5 1
4 5 2

Sample output Sample Output

5

Data range and prompt Data Size & Hint

[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.


You can use SPFA for this question, but you need to modify some details in the SPFA algorithm! Use SPFA to obtain the minimum vertex weight from vertex 1 to vertex I, from n to I, the largest point right (because this will make the point in the trade scheme to sell the crystal ball behind the point in the buy crystal ball), the edge is no longer entitled, the point with the right, SPFA, use the array maxCost [I] to indicate the maximum vertex weights from n to I, and minCost [I] to indicate the smallest vertex weights from 1 to n, and then perform the relaxation operation!

After SPFA is complete, locate A to make maxCost [B]-minCost [A] the largest, that is, the answer.

# Include <iostream> # include <string. h> # include <stdio. h> # include <stdlib. h >#include <queue> # define MAXV 500000 # define MAXE 100000 # define INF 0x3f3f3fusing namespace std; queue <int> q; struct edge {int u, v, next ;} edges [MAXV], edges2 [MAXV]; // forward edge table, reverse edge table int head [MAXE], last [MAXE], nCount = 0, nCount2 = 0, n, m; int minCost [MAXE], maxCost [MAXE]; // minCost [I] = the smallest edge between 1st and I, maxCost [I] = maximum edge int value [MAXE]; bool inQueue [MA XE]; // inQueue [I] = true indicates that point I is in the queue void AddEdge (int U, int V) {edges [++ nCount]. u = U; edges [nCount]. v = V; edges [nCount]. next = head [U]; head [U] = nCount;} void AddEdge2 (int U, int V) {edges2 [++ nCount2]. u = U; edges2 [nCount2]. v = V; edges2 [nCount2]. next = last [U]; last [U] = nCount2;} int max (int a, int B) {if (a> B) return a; return B ;} int min (int a, int B) {if (a <B) return a; return B;} void SPFA1 () {while (! Q. empty () q. pop (); minCost [1] = value [1]; memset (inQueue, false, sizeof (inQueue); q. push (1); inQueue [1] = true; while (! Q. empty () {int u = q. front (); q. pop (); inQueue [u] = false; for (int p = head [u]; p! =-1; p = edges [p]. next) {int v = edges [p]. v; if (minCost [u]> value [v] | minCost [v]> minCost [u]) {minCost [v] = min (value [v], minCost [u]); if (! InQueue [v]) {q. push (v); inQueue [v] = true ;}}}} void SPFA2 () {while (! Q. empty () q. pop (); maxCost [n] = value [n]; memset (inQueue, false, sizeof (inQueue); q. push (n); inQueue [n] = true; while (! Q. empty () {int u = q. front (); q. pop (); inQueue [u] = false; for (int p = last [u]; p! =-1; p = edges2 [p]. next) {int v = edges2 [p]. v; if (maxCost [u] <value [v] | maxCost [v] <maxCost [u]) {maxCost [v] = max (value [v], maxCost [u]); if (! InQueue [v]) {q. push (v); inQueue [v] = true ;}}} int main () {int ans =-1; memset (head,-1, sizeof (head); memset (last,-1, sizeof (last); memset (minCost, INF, sizeof (minCost); memset (maxCost,-1, sizeof (maxCost); scanf ("% d", & n, & m); for (int I = 1; I <= n; I ++) scanf ("% d", & value [I]); for (int I = 1; I <= m; I ++) {int a, B, c; scanf ("% d", & a, & B, & c); AddEdge (a, B); AddEdge2 (B, ); if (c = 2) {AddEdge (B, a); AddEdge2 (a, B) ;}} SPFA1 (); SPFA2 (); for (int I = 1; I <= n; I ++) if (ans <maxCost [I]-minCost [I]) ans = maxCost [I]-minCost [I]; printf ("% d \ n", ans); return 0 ;}

Zookeeper
What are the algorithm details?

Common algorithms and policies
Chapter 1 concepts of Algorithms
Chapter 2 Recursion
Chapter 3 backtracking
Chapter 4 sorting
Chapter 5 Search
Chapter 6 exhaustive strategy
Chapter 7 greedy Algorithms
Chapter 8 Governance Policy
Data Structure
Chapter 1 What is Data Structure
Chapter 2 linear table
Chapter 3 Stack
Chapter 4 team
Chapter 5 tree
Chapter 6
Dynamic Planning
Chapter 1 What is dynamic planning
Chapter 2 use dynamic planning to solve problems
Chapter 3 typical examples and exercises
Chapter 4 recursive functions of Dynamic Planning
Chapter 5 dynamic planning Classification 1
Mathematical Knowledge and Related Algorithms
Chapter 1 algorithms related to Number Theory
Chapter 2 high-precision computing
Chapter 3 Arrangement and combination
Chapter 4 computational ry
Chapter 5 Other mathematical knowledge and Algorithms
Graph Algorithm
Chapter 1 minimal spanning tree
Chapter 2 Shortest Path
Chapter 3 topological sorting (AOV Network)
Chapter 4 key paths (AOE Network)
Chapter 5 Network Flow
Chapter 6 Graph Matching
Search Algorithms and Optimization
Chapter 1 bidirectional breadth-first search
Chapter 2 branch demarcation
Chapter 3 A * Algorithm

Pascal: 'start from there'

1 Statement
2. Data Structure + algorithm + vijos for Water Problems
3. A large number of typical usaco questions should be at least 4.0 +, and others will volunteer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.