Difference Constraint
Suppose there is a sequence like this: S = {A1, A2, A3, a4... AI...}
Here, AI = A * si, which can be ignored.
Now, an inequality is given so that ai + a (I + 1) + a (I + 2) +... + a (I + n) <Ki Or AI + a (I + 1) + a (I + 2) +... + a (I + n)> Ki
First, two numbers represent the number of S sequences and the number of inequalities.
The inequality can be described in this way.
Given four parameters, the first number I can represent the number of the sequence, and then gives n, so that the first two numbers can be described as AI + a (I + 1) +... A (I + n), that is, the continuous sum from I to N, then a symbol and a ki
If the symbol is GT, it indicates '>', and if the symbol is lt, it indicates '<'
The sample can represent
1 2 GT 0
A1 + A2 + A3> 0
2 2 lt 2
A2 + A3 + A4 <2
Finally, I would like to ask if all your inequalities meet the conditions. If the output lamentable Kingdom does not meet the output successful conspiracy, I should pay attention to it. Do not reverse it.
Solution: a typical difference constraint makes it easy to introduce a constraint inequality.
First set Si = A1 + A2 + A3 +... + AI
According to the example
S3-S0> 0 ----> S0-S3 <=-1
S4-S1 <2 ----> S4-S1 <= 1
Because the condition of the difference constraint is less than or equal to, we can get an equal sign for the ki-1.
The general expression can be expressed
A B Gt C
S [A-1]-s [A + B] <=-ki-1
A B lt C
S [A + B]-s [A-1] <= ki-1
Create a Graph Based on the difference constraint and add these directed edges.
GT: <A + B, A-1> =-ki-1
LT: <A-1, A + B> = ki-1
Then, judge whether there is a negative ring based on bellman_ford.
Only when all inequalities meet the conditions (P condition) will negative loops not appear (Q conclusion ).
P> q ,! Q-> P
If a negative ring exists (! Q), indicating the contradiction, then this sequence does not satisfy all the inequalities (! P ).