#1731. [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 n (2n103)n \ (2 \le n \le 10^3) cows numbered 1n1\dots 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 (1ml104)ml \ (1 \le ml \le 10^4) constraints describes which cows like each other and the maximum distance by which they may be separated;a subsequent list of mdmd constraints  (1md104)\ (1 \le md \le 10^4) 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 11 and cow nn that satisfies the distance constraints.

Input Format

  • Line 11: Three space-separated integers: n,mln,ml, and mdmd.
  • Lines 2ml+12 \ldots ml+1: Each line contains three space-separated positive integers: a,ba,b, and dd, with 1a<bn1 \le a < b \le n. Cows aa and bb must be at most d (1d106)d \ (1 \le d \le 10^6) apart.
  • Lines ml+2ml+md+1ml+2 \ldots ml+md+1: Each line contains three space-separated positive integers: a,ba,b, and dd, with 1a<bn1 \le a < b \le n. Cows aa and bb must be at least d (1d106)d \ (1 \le d \le 10^6) apart.

Output Format

  • Line 11: 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 11 and nn.
4 2 1
1 3 10
2 4 20
2 3 3
27

There are 44 cows. Cows 11 and 33 must be no more than 1010 units apart, cows 22 and 44 must be no more than 2020 units apart, and cows 22 and 33 dislike each other and must be no fewer than 33 units apart.