[Modify spfa] optimal trade

Source: Internet
Author: User

[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 uses this trade method to earn travel expenses: he will choose a city that passes by to buy his favorite goods-the crystal ball and sell it 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 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]

One count: the most profitable travel expenses.

[Example 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]

5

[Analysis]

Spfa.

# Include <stdio. h> # include <string. h> # include <limits. h> # define maxn 100010 struct tnode {int num; tnode * Next;} A [maxn], B [maxn], * t; // a normal B inverted int N, m, X, Y, Z, L, R, ans; int f [maxn], G [maxn], Q [maxn * 3], W [maxn]; // F maximum g minimum bool V [maxn]; void insert (int x, tnode & P) {T = new (tnode); t-> num = X; t-> next = P. next; p. next = T;} int main () {scanf ("% d", & N, & M); For (INT I = 1; I <= N; ++ I) scanf ("% d", & W [I]); for (in T I = 1; I <= m; ++ I) {scanf ("% d", & X, & Y, & Z); insert (Y, A [x]); insert (X, B [y]); If (Z = 2) {insert (X, a [y]); insert (Y, B [x]) ;}}for (INT I = 1; I <= N; ++ I) g [I] = int_max; L = r = 0; Q [0] = 1; V [1] = 1; G [1] = W [1]; while (L <= r) {x = Q [l]; T = A [X]. next; while (T! = NULL) {Y = T-> num; If (G [x] <G [y]) {G [y] = G [x]; if (W [y] <G [y]) g [y] = W [y]; If (! V [y]) {q [++ R] = y; V [y] = 1 ;}t = T-> next ;}++ L; V [x] = 0;} memset (v, 0, sizeof (0); L = r = 0; Q [0] = N; V [N] = 1; f [N] = W [N]; while (L <= r) {x = Q [l]; t = B [X]. next; while (T! = NULL) {Y = T-> num; If (F [x]> F [y]) {f [y] = f [x]; if (W [y]> F [y]) f [y] = W [y]; If (! V [y]) {q [++ R] = y; V [y] = 1 ;}t = T-> next ;}++ L; V [x] = 0;} For (INT I = 1; I <= N; ++ I) if (F [I]-G [I]> ans) ans = f [I]-G [I]; printf ("% d \ n", ANS); Return 0 ;}

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.