POJ 3169 Layout (SPFA algorithm solves the difference constraint)
Layout
Time Limit:1000 MS |
|
Memory Limit:65536 K |
Total Submissions:8975 |
|
Accepted:4308 |
Description
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.
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.
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 11 3 102 4 202 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.
Question: nheaded ox, numbered 1 ~ N. In the order of numbers. There is A good relationship between them, so we hope that the distance between them will not exceed A certain degree. Given ML indicates there are ML pairs (A, B, D ), the maximum distance between A and B is D. There are also some cool relationships, and the distance between them must be greater than a certain distance. Given DL represents a dl pair (A, B, D), indicating that the minimum distance between A and B is D. In addition, multiple bulls may stand in the same position. Calculate the distance between OX 1 and ox N. If no sorting method meets the conditions, output-1. Output 2 in infinite cases.
Question: The difference constraint problem is converted into the shortest-circuit solution. Set the I header to the position of dis [I. Ask the ox to stand in order. We can know that dis [I + 1]> = dis [I], and the weight from I + 1 to I is 0. Based on the given ML information, we can get dis [MA] + MD> = dis [MB], so that the weight from MA to MB is MD (note: is a unidirectional full graph ). Based on the DL information of the given relationship difference, we can obtain dis [DA] + DD <= dis [DB], so that the weight from DB to DA is DD. Create a graph based on these three constraints (inequalities. The longest distance between OX 1 and ox N is the shortest path in the figure. However, the figure may contain negative weights, so the dijsktra algorithm cannot be used. The SPFA algorithm can determine the negative ring. If a negative ring exists, it indicates that no matching arrangement exists.
The Code is as follows:
# Include
# Include
# Include
# Define maxn 1010 # define maxm 50010 # define INF 0x3f3f3fusing namespace std; int dis [maxn], visit [maxn], mark [maxn], top, n; int head [maxn]; struct node {int to, val, next;} edge [maxm]; void add (int a, int B, int c) {edge [top]. to = B; edge [top]. val = c; edge [top]. next = head [a]; head [a] = top ++;} void spfa () {int I, v, u, mark [maxn]; queue
Q; for (I = 1; I <= n; ++ I) {dis [I] = INF; visit [I] = 0; mark [I] = 0 ;} q. push (1); dis [1] = 0; visit [1] = 1; mark [1] = 1; // record the number of incoming queues while (! Q. empty () {v = q. front (); q. pop (); visit [v] = 0; for (I = head [v]; I! =-1; I = edge [I]. next) {u = edge [I]. to; if (dis [u]> dis [v] + edge [I]. val) {dis [u] = dis [v] + edge [I]. val; if (! Visit [u]) {visit [u] = 1; mark [u] ++; q. push (u); if (mark [u]> = n) // negative ring exists {printf ("-1 \ n"); return ;}}}}} if (dis [n] = INF) // indicates that the maximum distance can be infinitely large printf ("-2 \ n"); elseprintf ("% d \ n ", dis [n]);} int main () {int ML, MD, u, v, d, I; while (scanf ("% d ", & n, & ML, & MD )! = EOF) {top = 0; memset (head,-1, sizeof (head); for (I = 1; I
Zookeeper