// Differential constraint system <br/> // The establishment of the inequality relationship is very intuitive. For example: <br/> // The distance between S1 and S2 cannot be greater than 10 <br/> // S2-S1 <= 10 <br/> // The distance between S3 and S4 cannot be less than 15 <br/> // S4-S3> = 15 <br/> // start with 1 after the diagram, calculate the shortest path of spfa <br/> // If the spfa cannot reach the end point N: If cows 1 and N can be arbitrarily far apart, output-2 <br/> // If a loop exists: If no line-up is possible, output-1 <br/> # include <iostream> <br/> # include <queue> <br/> # define INF 1000000000 <br/> # define maxm 20010 <br/> # define maxn 1010 <br/> USI Ng namespace STD; <br/> int m, n, ML, MD, U, V, W; <br/> int head [maxn], V [maxm], W [maxm], next [maxm]; <br/> int CNT [maxn], DIS [maxn]; <br/> bool INQ [maxn]; <br/> void addedge (int u, int V, int W) // Add edge <br/> {<br/> V [m] = V; <br/> W [m] = W; <br/> next [m] = head [u]; <br/> head [u] = m ++; <br/>}< br/> void buildgraph () // diagram <br/> {<br/> scanf ("% d", & N, & ml, & MD); <br/> memset (Head,-1, sizeof (head); <br/> m = 0; <br/> while (Ml- -) <Br/>{< br/> scanf ("% d", & U, & V, & W); <br/> addedge (u, v, W); <br/>}< br/> while (MD --) <br/>{< br/> scanf ("% d ", & U, & V, & W); <br/> addedge (v, U,-W ); <br/>}< br/> int spfa () <br/>{< br/> memset (CNT, 0, sizeof (CNT )); <br/> memset (INQ, 0, sizeof (INQ); // whether or not it is in the queue <br/> for (INT I = 2; I <= N; ++ I) dis [I] = inf; // initialization <br/> queue <int> q; <br/> dis [1] = 0; <br/> q. push (1); // 1 as the starting point <br/> INQ [1] = 1; <br/> while (! Q. empty () <br/>{< br/> int u = Q. front (); q. pop (); <br/> INQ [u] = 0; <br/> for (int e = head [u]; e! =-1; E = next [e]) <br/>{< br/> If (DIS [u] + W [e] <dis [V [e]) <br/> {<br/> dis [V [e] = dis [u] + W [E]; <br/> If (! INQ [V [e]) <br/>{< br/> q. push (V [e]); <br/> INQ [V [e] = 1; <br/> If (++ CNT [V [e]> N) return-1; // if the number of queues exceeds n, a loop exists, back to-1 <br/>}< br/> If (DIS [N] = inf) return-2; // inaccessible, output-2 <br/> else return dis [N]; // The shortest distance from the returned result to the end is the answer <br/>}< br/> int main () <br/>{< br/> // freopen ("in.txt", "r", stdin); <br/> buildgraph (); <br/> printf ("% d/N", spfa (); <br/>}