bzoj#P1731. [USACO 2005 Dec] Layout 排队布局
[USACO 2005 Dec] Layout 排队布局
Description
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has cows numbered 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 constraints describes which cows like each other and the maximum distance by which they may be separated;a subsequent list of constraints 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 and cow that satisfies the distance constraints.
Input Format
- Line : Three space-separated integers: , and .
- Lines : Each line contains three space-separated positive integers: , and , with . Cows and must be at most apart.
- Lines : Each line contains three space-separated positive integers: , and , with . Cows and must be at least apart.
Output Format
- Line : 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 and .
4 2 1
1 3 10
2 4 20
2 3 3
27
There are cows. Cows and must be no more than units apart, cows and must be no more than units apart, and cows and dislike each other and must be no fewer than units apart.