Poj00009 (minimum short circuit + differential constraint)

Source: Internet
Author: User

Poj00009 (minimum short circuit + differential constraint)
Layout

Time Limit:1000 MS Memory Limit:65536 K
Total Submissions:7613 Accepted:3658

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.

Source

USACO 2005 December Gold



There are nheaded cows in a barn, and some cows have a good relationship. They hope they can be at least a certain distance from each other. Of course, there are also some cool relationships, and they want to be more distant from each other. There is a good relationship between ML and ox, and the distance D between each ox is given. Similarly, there is a bad relationship between MD and ox, and the distance D between each ox is given. Ask if there are any solutions that meet the requirements of all cattle. If not, output-1. If yes, but the distance between OX 1 and ox N can be any large, output-2; otherwise, output the maximum distance between OX 1 and ox N.


Note: The place where the bull number I is located is d [I]. First, the bull market is ranked by serial number. Therefore, d [I] <= d [I + 1] is established. Second, d [AL] + DL> = d [BL], similarly, d [AD] + DD <= d [BD] is the minimum distance limit for cattle with poor relationships. In this way, the original problem can be transformed into solving the maximum value problem of d [N]-d [1] When three inequality groups are met. Of course, linear programming can be used to solve the problem. However, this question can be solved in a simpler way. This can be abstracted as an undirected graph. Each ox is a vertex. The good or bad relationship between them is an edge, and the restricted distance is used as the edge Sund weight. In this way, we can convert these three inequalities: d [I + 1] + 0> = d [I] indicates that an edge with a weight of 0 is connected from I + 1 to I, d [AL] + DL> = d [BL] indicates that an edge with the weight of DL is connected from AL to BL, d [BD]-DD> = d [AD] indicates that an edge with a weight of-DD is connected from BD to AD. The maximum value of d [N]-d [1] corresponds to the shortest path between d [1] and d [n. Because of the negative weight edge, the bellman-ford algorithm is used.

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include
            
              Using namespace std; # define INF 123456789 # define LL long # define MID (a, B) a + (B-a)/2 const int maxn = 1000 + 10; const int maxm = 10000 + 10; int al [maxm], bl [maxm], dl [maxm]; int ad [maxm], bd [maxm], dd [maxm]; int d [maxn]; int N, ML, MD; void solve () {fill (d, d + N, INF ); // initialize d [] d [0] = 0; for (int I = 0; I
             
              

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.