Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).
Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated.
Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.
Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated.
Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.
Input
Line 1: Three space-separated integers: N, ML, and MD.
Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.
Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.
Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
Output
Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.
Sample Input
4 2 1 1 3 10 2 4 20 2 3 3
Sample Output
27
Hint
Explanation of the sample:
There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
M and n have the meaning of problems between the two cases of s points, dots, n denotes a maximum between points between l, m represents the minimum point and the point L, Q 1 to the maximum value between s how much time.
Ideas: The meaning of the questions can be obtained: n-case d [1] - d [3] <= 10, d [4] - d [2] <= 20, m where: d [3] - d [ 2]> = 3, by conversion to give a third equation d [2] - d [3] <= - 3. So that Italy is quite right to seek the most negative short-circuited.
Code:
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 998244353 16 #define eps 1e-6 . 17 #define LL Long Long 18 is #define INF 0x3f3f3f3f . 19 the using namespace STD; 20 is 21 is // structure stored side information 22 is struct Node 23 is { 24 int Y, Z, Next; 25 }; 26 is Node NO [ 10005 ]; 27 // storage position edge 28 int head [ 10005 ]; 29 // S represents the number of points, n is the number of ML represents the edge, m represents the number of edges of MD 30 int S, n, m; 31 is // ANS denotes the number of edges 32 intANS; 33 is // represents the minimum value of the starting point to the other point 34 is int DIS [ 10005 ]; 35 // determines whether the current minimum value of the point found 36 BOOL VIS [ 10005 ]; 37 [ // record the current point to find several 38 int CNT [ 10005 ]; 39 // find the shortest and determines whether there is a negative ring 40 BOOL SPFA () 41 is { 42 is // initialization 43 is Queue < int > Qu; 44 is qu.push ( . 1 ); 45 Memset (DIS, INF , sizeof(DIS)); 46 is Memset (VIS, 0 , the sizeof (VIS)); 47 Memset (CNT, 0 , the sizeof (CNT)); 48 // initializes the starting point 49 VIS [ . 1 ] = . 1 ; 50 DIS [ . 1 ] = 0 ; 51 // queue is empty exit 52 is the while (! qu.empty ()) 53 is { 54 is // dequeue 55 int U = qu.front (); 56 is qu.pop (); 57 is //Since the minimum value of the point has changed, so the value of the points connected thereto also changes 58 VIS [u] = 0 ; 59 // edge traversal connected with u 60 for ( int I = head [u]; ! = I - . 1 ; I = NO [I] .next) 61 is { 62 is int V = NO [I] .y; 63 is // updated minimum 64 IF (DIS [V]> DIS [U] + NO [I ] .Z) 65 { 66 DIS [v] = DIS [U] + NO [I] .Z; 67 // if the current point v does not need to look for the labeled 68 IF ! ( VIS [v]) 69 { 70 // marker point v 71 is VIS [v] = . 1 ; 72 qu.push (v); 73 is // number of times the search record store 74 CNT [v] ++ ; 75 // if the number is greater than n represents a negative ring 76 IF (CNT [V]> n-) 77 { 78 return to false ; 79 } 80 } 81 } 82 } 83 } 84 } 85 int main() 86 { 87 scanf("%d %d %d",&s,&n,&m); 88 int u,v,l; 89 ans=1; 90 //初始化 91 memset(head,-1,sizeof(head)); 92 //正向记录边 93 for(int i=1;i<=n;i++) 94 { 95 scanf("%d %d %d",&u,&v,&l); 96 no[ans].y=v; 97 no[ans].z=l; 98 no[ans].next=head[u]; 99 head[u]=ans++; 100 } 101 //反向记录边 102 for(int i=1;i<=m;i++) 103 { 104 scanf("%d %d %d",&u,&v,&l); 105 no[ans].y=u; 106 no[ans].z=-l; 107 no[ans].next=head[v]; 108 head[v]=ans++; 109 } 110 // If there is no loop negative output solutions -1 infinite solutions there are many cases showing output -2, there is a solution output solution 111 IF (SPFA ()) 112 { 113 IF (DIS [S] == INF) 114 { 115 the printf ( " -2 \ n- " ); 1 16 } 117 the else 1 18 { 119 the printf ( " % D \ n- " , DIS [S]); 120 } 121 } 122 the else 123 { 124 the printf ("-1\n"); 125 } 126 127 }