Limit path1364 difference Constraint

Source: Internet
Author: User

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 ).

 

 

 

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.