Differential constraint system

Source: Internet
Author: User

 

 

Training address

 

Summary:
First:
The difficulty lies in creating images.


Second:
①: For a difference inequality, a-B <= c creates an edge with the weight from B to a being C. The result is the shortest path and the maximum value is obtained.
②: For the inequality a-B> = C, create an edge with the weight from B to a being C, and obtain the longest path and the minimum value.
③: If a negative ring exists, there is no solution.
④: If you cannot find the shortest path (Dist [] is not updated), it is an arbitrary solution.


Third:
A method for creating images:
If X [I] is the value of position I (or time) (the same as the attribute of the value), then X [I] is regarded as a series, X [I] = s [I]-s [I-1];
So we can establish at least a relationship similar to this: 0 <= s [I]-s [I-1] <= 1;
Other relationships are subject to question exploration.

The above is reproduced here

 

 

Question 1:

Enter n students and M for comparison: A, B, C indicates that student a thinks student B cannot get more candy than him. What is the maximum number of sweets between student 1 and student n?

Analysis: directly create a graph based on B-A <= C.

Question 2:

Let's give you a sequence starting point Si and sequence length Ni, as well as their range and the relationship with the given number Ki size. Then, ask if you have such a relationship?

Analysis: (1) it is not the starting and ending points of the sequence, but the starting point and the sequence length. Therefore, the interval is [Si, si + N], which is prone to errors.

(2) To> and <relationship, through the Ki-1 to <= and> = relationship.

(3) Si + 1 + ..... + Si + n <ki can be converted to sum (I + n)-sum (I-1) <ki to satisfy the difference constraint system.

(4) The source point is not specified in the question. Therefore, set n + 1 as the super source point and run spfa from the Super source point.

 

 1 tot = 0; 2 _Clr(head, -1); 3 while(m--) 4 { 5     scanf("%d%d%s%d", &a, &b, p, &c); 6     if(p[0]==‘g‘) 7         Add_edge(a+b, a-1, -c-1); 8     else 9         Add_edge(a-1, a+b, c-1);10 }11 int S = n+1;12 for(int i=0; i<=n; i++)13     Add_edge(S, i, 0);

 

 

 

Third question:

Give you n integer intervals [a, B]. Let you find the smallest set Z so that the intersection of Z and each interval has at least two different elements. What is the minimum Z size?

Analysis: Si represents the number of integers in the range [0, I]. From the question, we can see that any interval [a, B] should contain at least two elements; that is, Sb-Sa-1> = 2.

There is also an implicit condition: because it is an integer interval, then any adjacent two intervals Si and Si-1 meet the relationship: 0 <= Si-Si-1 <= 1.

You also need to add a source point so that you can find the minimum value, so you can find the longest path.

1 tot = 0; 2 _ CLR (Head,-1); 3 while (M --) 4 {5 scanf ("% d", & A, & B ); 6 add_edge (A, B + 1, 2); 7 N = max (n, B + 1); 8} 9 S = N + 3; // The same 10 for (INT I = 0; I <n; I ++) 11 {12 add_edge (I + 1, I, -1); 13 add_edge (I, I + 1, 0); 14 add_edge (S, I, 0); 15}

 

Fourth question:

Basically the same as the above mentioned, that is, the intersection is no longer 2, but CI. the start and end points are the leftmost and rightmost points of the entire interval.

 1 tot = 0; 2 _Clr(head, -1); 3 for(int i=0; i<n; i++) 4 { 5     scanf("%d%d%d", &a, &b, &c); 6     Add_edge(a, b+1, c); 7     st = min(a, st); 8     ed = max(b+1, ed); 9 }10 for(int i=st; i<=ed; i++)11 {12     Add_edge(i, i+1, 0);13     Add_edge(i+1, i, -1);14 }

Fifth question:

There is a line of nheaded milk steak, and they all like to lean closer to the relationship as much as possible (allowing multiple cows to stand at the same point ). ML friendly relationship a, B, d indicates that a and B can only be separated by D at the farthest distance; and MD anti-relationship a, B, d indicates that a and B must be separated by D at least. Q: What is the maximum distance between cow 1 and cow n? If a queue cannot be output-1, if they can be infinitely large, then output-2.

Analysis: Using Si to represent the distance between I and 1, then the graph relationship has been obvious, but there is a more implicit condition: Si-Si-1> = 0. Because the adjacent two can stand in the same position.

1 tot = 0; 2 _ CLR (Head,-1); 3 for (INT I = 0; I <ml; I ++) 4 {5 scanf ("% d", & A, & B, & C); 6 add_edge (A, B, C ); 7} 8 for (INT I = 0; I <md; I ++) 9 {10 scanf ("% d", & A, & B, & C); 11 add_edge (B, A,-C); 12} 13 for (INT I = 1; I <= N; I ++) // However, this implicit condition can also be removed. Alas ...... 14 add_edge (I + 1, I, 0)

Sixth Question:

It won't be done yet. Keep it first .....

 

7. Question:

N employees and M employees. There are two formats of information: p a B x indicates that A is located at the north side of B x lightyears; v a B indicates that A is located at least one light year in the north side of B. Then, let you determine whether the M information is reliable or not?

Analysis: The S (I) is used to indicate the distance between the I and the north of the S. For p a B x, S (a)-S (B) = x; can be converted to: S (a)-S (B)> = x & S (a)-S (B) <= x to create a graph.

S is a self-established source point.

 1 tot = 0; 2 _Clr(head, -1); 3 while(m--) 4 { 5     scanf(" %c%d%d", &p, &a, &b); 6     if(p==‘P‘) 7     { 8         scanf("%d", &c); 9         Add_edge(b, a, c);10         Add_edge(a, b, -c);11     }12     else13         Add_edge(a, b, -1);14 }15 for(int i=1; i<=n; i++)16     Add_edge(0, i, 0);

Eighth Question:

A ninja jumped in a row of trees. He jumped strictly in the order of height from height to height, but there was a limit on the distance between him and D. How long is the longest distance between the dwarf tree and the highest tree?

Analysis: Is there a need to note whether he jumps from left to right or from right to left? There is also an implicit constraint: each tree is at an integer point, that is, the closest distance between the two trees is 1, that is, Si-Si-1> = 1.

1 tot = 0; 2 _ CLR (Head,-1); 3 for (INT I = 1; I <= N; I ++) 4 {5 scanf ("% d", & tree [I]. h); 6 tree [I]. id = I; 7} 8 sort (tree + 1, tree + n + 1); 9 for (INT I = 1; I <n; I ++) 10 {11 int u = tree [I]. ID; 12 INT v = tree [I + 1]. ID; 13 if (u> V) // jump from left to right 14 add_edge (v, U, d); 15 else // vice versa. 16 add_edge (u, v, d); 17 add_edge (I + 1, I,-1); // The adjacent two course trees cannot be in the same position. 18} 19 int ST = tree [1]. ID, ED = tree [N]. ID; // locate the shortest tree and the highest tree

 

Differential constraint system

Related Article

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.