POJ 3169 Layout (differential constraint system)

Source: Internet
Author: User

POJ 3169 Layout (differential constraint system)

 

Question: the number of the nheaded ox is 1 to n, which is arranged in the order of numbers. The distance between the two cows is greater than or equal to 0. There are some constraints on the distance between these cows: 1. there is a constraint relationship between ML groups (u, v, w), indicating that the distance between Ox [u] And Ox [v] Must be <= w. 2. There is a binding relationship between the MD group (u, v, w), indicating that the distance between the ox [u] And the ox [v] must be greater than or equal to w. If the nheader cannot be lined up, the output is-1. If the distance between Ox [1] and ox [n] is infinitely far, the output is-2, otherwise, the maximum distance between Ox [1] and ox [n] is output.

This is my first difference constraint. In fact, this linear plan can still be converted into a graph theory short-circuit model, which is 666.

I have reproduced an article about the difference constraint, which is very detailed at www.bkjia.com.

Now let's take a look at the cognition of this model conversion. First, the single-source shortest model conforms to the inequality d (v)-d (u) <= w (u, v) with difference constraints ), therefore, finding a group of inequalities is equivalent to solving the dis array. After a source point src is set, the dis [src] is set to 0, finally, the dis array is relaxed from infinity to the inequality that satisfies the difference constraint by using various short circuit algorithms. Therefore, we can draw this conclusion:For a differential constraint system with an unknown number set to death (dis [src] = 0), all the unknown numbers reach the maximum value in a group of solutions obtained through the shortest path algorithm.

When we use the same idea to find the longest path and initialize the dis array to a negative infinity, set a source point (that is, one of the solutions is a fixed value of 0) the unknown values of the calculated solutions all reach the minimum value.

So for this question, first we need to find the difference constraint system and try to use an inequality between two unknowns to represent the distance between cows. Therefore, we can use coordinates to represent the positions of cattle, set A coordinate origin, which must be on the left of the ox 1, so the unequal relations between the positions of cattle are: X (Bl)-X (Al) <= E (A, B) = Dl X (Ad)-X (Bd) <= E (B, A) =-Dd, And the ox must be sorted in the order of numbers, so there are: X (I)-X (I + 1) <= E (I + 1, I) = 0. then the question asks you to calculate the maximum distance between a cow and a cow, that is, max (X (N)-X (1 )). Therefore, the solution X (1) of No. 1 is fixed to death. In a group of solutions obtained by the shortest path algorithm, all unknown numbers reach the maximum value. Therefore, X (N) -X (1) is the maximum value. Therefore, you can set a fixed distance to the source point on the left of OX 1, initialize dis [1] = a, and calculate the shortest path, obtain the answer dis [N]-dis [1],

So it is best to set the value of a to 0 (as long as it is a fixed value ).

Code implementation:

 

//268K172MS#include
 
  #include
  
   #include
   
    #include#define inf 0x7fffffffusing namespace std;int n,ml,md,flag;int u[20100],v[20100],w[20100];int dis[1100];void bellman(){    fill(dis,dis+n+1,inf);    dis[1]=0;    for(int i=1;i
    
     dis[u[i] ]+w[i]) flag=1;    if(flag) printf(-1);    else if(dis[n]==inf) printf(-2);    else printf(%d,dis[n]);    return 0;}
    
   
  
 
At this point, we will solve this question. The following is the tangle of detail between the weak and the weak... You can ignore it.

 

This problem may involve negative weights or disconnections (dis [n] = inf) in many situations ), or the source point is not connected to the negative full ring (in this case,-1 instead of-2 should be output based on the question)

If you think of this, you can hack a lot of ac code (including me). For example, if the source point is not connected to the negative full ring, it will be useless to check whether a negative full ring exists in another round loop, because I used "dis [u [I]! = Inf, however, we need to determine that this question is not connected (dis [n] = inf). Therefore, we must maintain the absolute maximum value of inf and cannot participate in the relaxation process. Therefore, the two cannot have both sides, you can use bellman only once to determine a negative weight ring that is not connected to the source point. This is a little clumsy.

Of course, in the normal SPFA, the negative weight ring that is not connected to the source point cannot be detected. However, here is a tip. Before using SPFA, You can team up each vertex in the graph in advance and execute an algorithm to check whether a vertex has been in the queue for more than n times, at this time, inf is not involved, which can be connected or the negative weight ring of the entire separation graph.

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.