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 ;}