NYOJ 247 virtual city tour

Source: Internet
Author: User

Virtual city tour time limit: 3000 MS | memory limit: 65535 KB difficulty: 6
Description

Pavilions are the epitome of future cities, and personal experience and interaction are the same theme. Through multi-dimensional mode and high-tech means, visitors can embark on A virtual city tour in the exhibition space. Dream has N cities and M roads in China, and each road connects two cities. A maximum of one road can be directly connected between any two cities. Some of these M roads are unidirectional roads and some are bidirectional roads. With a vast territory and different resource distribution in different regions, the prices of the same product may not be the same in different cities. However, the prices and prices of the same product in the same city are always the same. Now you are on a journey to a virtual city. In order to give you an unexpected harvest, you can use the price difference of X products in different cities to earn a little travel expenses while traveling, but you can only trade it once at most. That is, if you buy X products in a city, you can purchase them in another city for travel expenses. Of course, if you do not make any difference, you may not conduct trade activities. The number of N cities in the dream country ranges from 1 ~ N. You can only start from City 1 and end your 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. For example, 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. Assume that X products are in the range of 1 ~ The prices of city 5 are 4, 3, 5, 6, and 1, respectively. You can choose the next line: 1235, and buy X goods at the price of 3 in City 2, sell X goods at the price of 5 in City 3, earning 2 travel expenses. You can also choose to buy X products at the price of 1 when the next line reaches city 5 14545, when 2nd times reaches City 4, X products are sold at the price of 6, earning 5 travel expenses. The price of X commodities in N cities and the information of M roads are provided (numbers of the two cities connected by each road and the traffic conditions of the road ). Can you earn as many travel expenses as possible.
Input
There are multiple groups of test data (marked by the end of the EOF file)
The format of each group of test data is as follows:
The first line: n m represents the number of cities and the number of roads, respectively.
Row 2: N positive integers separated by a space, representing the prices of goods in 1 to N cities.
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, this road is a two-way road between city X and city Y.

1 ≤ N ≤ 100000,1 ≤ M ≤ 500000,
1 ≤ X, Y ≤ N, 1 ≤ Z ≤ 2, 1 ≤ commodity price ≤ 100.
Output
Output an integer to indicate the maximum amount of travel expenses that can be earned. If there is no trade, 0 is output.
Sample Input
5 54 3 5 6 11 2 11 4 12 3 23 5 14 5 2
Sample output
5
Source
Third Henan Program Design Competition

Find the minimum value to reach each vertex from point s, and then find the maximum value to reach each vertex from point t,
Then make the difference. Because it is to determine whether it can be reached, all vertices that must be checked are queued.

# Include "stdio. h" # include "string. h" # include "queue" using namespace std; # define max (a, B) (a> B? A: B) # define min (a, B) (
     
      
Q; int x, I, v; int mark1 [N], mark2 [N]; memset (mark1, 0, sizeof (mark1); memset (mark2, 0, sizeof (mark2); mark1 [s] = 1; mark2 [n] = 1; q. push (s); while (! Q. empty () {x = q. front (); q. pop (); for (I = head1 [x]; I! =-1; I = bian [I]. next) {v = bian [I]. v; a [v] = min (a [v], a [x]); if (! Mark1 [v]) {mark1 [v] = 1; q. push (v) ;}} q. push (n); while (! Q. empty () // returns the start point from the end point, that is, the reverse side {x = q. front (); q. pop (); for (I = head2 [x]; I! =-1; I = bian [I]. next) {v = bian [I]. v; B [v] = max (B [v], B [x]); if (! Mark2 [v]) {mark2 [v] = 1; q. push (v) ;}} int ans = 0; for (I = 1; I <= n; I ++) {if (mark1 [I] & mark2 [I]) {ans = max (ans, B [I]-a [I]) ;}} return ans ;} int main () {int n, m, u, v, x, I; while (scanf ("% d", & n, & m )! =-1) {for (I = 1; I <= n; I ++) {scanf ("% d", & a [I]); B [I] = a [I]; // a [I] records the minimum value, B [I] records the maximum value} memset (head1,-1, sizeof (head1 )); memset (head2,-1, sizeof (head2); e = 0; while (m --) {scanf ("% d", & u, & v, & x); add (u, v); if (x = 2) add (v, u);} printf ("% d \ n", spfa (1, n);} 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.