As you can see in the Acm thesis, learning together has at least solved many of my questions.
3.3 Example 3 -- Layout
Description:
When waiting in the queue for feeding, the cows like to stand close to their friends. FJ has N (2 <= N <= 1000) cows numbered from 1 to N, standing along a straight line waiting for feeding. The order of the cows in the group is the same as their numbers. Because the cows are quite slim, there may be two or more cows standing in the same position. Even if we imagine that a cow is standing on a digital axis, two or more cows are allowed to have the same horizontal coordinates.
Some cows have a crush on each other, and they want the distance between the two to not exceed a given number. On the other hand, some cows are very disgusted with each other and want the distance between the two to be no less than a given number of D. Give a description of the goodwill between two cows, and give a description of the resentment between the two cows. (1 <= ML, MD <= random, 1 <= L, D <= 1000000)
Your job is: if there is no solution that meets the requirements, output-1; if the number of cows 1 and number N
The distance between the cows can be any large, and the output is-2. Otherwise, the maximum distance between the cows 1 and the cows N is calculated if all requirements are met.
Analysis:
If the current problem is complicated, we should learn to "step back" and think from simple to complex.
I don't know where to start to find the maximum value. First, we can analyze it easily.
First, we will study how to output the maximum distance between 1 and N without a feasible distance.
We use D [I] to indicate the distance between dairy cows I and dairy cows 1.
Because the order in the team must be the same as the number, for any number I cows, 1 <= I <N, the distance should meet the following requirements:
D [I + 1]-D [I]> = 0
For the descriptions of each goodwill (I, j, k), assuming I <= j, the requirements for distance are:
D [j]-D [I] <= k
For each dislike description (I, j, k), suppose I <= j, which reflects the distance requirement:
D [j]-D [I]> = k
The model has a name called differential constraint system.
For convenience, we write each type of inequality in the agreed form:
D [I] <= D [I + 1]
D [j] <= D [I] + k
D [I] <= D [j]-k
What do you think of when you see these inequalities?
Yes, we have such an inequality in the problem of finding the shortest path between vertices:
If vertex u to vertex v has edges e = uv, And the edge weight is w (e), set d (u), d (v) the shortest path length from the source point to the vertex u and vertex v,
Then d (v) <= d (u) + w (e)
This inequality is very similar to the previous conditional form, which inspires us to use the most short circuit in the diagram.
The procedure is as follows:
G = (V, E), V = {v1, v2, v3 ,..., Vn}, E = {e1, e2, e3 ,...}, For adjacent two points I and (I + 1), the corresponding vertex vi + 1 leads an edge to vi at a cost of 0. For each group of preference descriptions (ai, bi, di ), we assume that there is ai <bi. Otherwise, the ai and bi exchange, then the vertex vai leads an edge to vbi at the expense of di. dislike descriptions for each group (ai, bi, di ), we assume there is ai <bi. Otherwise, the ai and bi exchange, then the vertex vbi leads an edge to vai at the cost of-di.
So the problem becomes to find the shortest path from v1 to all other vertices in G. We prove that if there is no negative weight loop in G, there is a solution to the problem, that is, there is a series meeting the conditions. If G has a negative weight loop, there is no solution to the problem, that is, there is no series meeting the conditions.
Theorem 5: Is there a solution equivalent to whether graph G has no negative weight loop.
Proof: If there is no negative weight loop in G, we can find the shortest path length of other vertices u in v1, and set it to d (u ). Because it is the shortest path, for any side eE, e = uv, d (u) + w (e)> = d (v ), thus, all constraints are met, and the problem must be solved. If G has a negative weight loop, it means that at any time, G has at least one vertex v's shortest path length which can be updated. Therefore, an edge e = uv must exist, so that d (u) + w (e) <d (v ). Therefore, at any time, a certain constraint condition is not met and the problem cannot be solved. (Certificate completed)
The Bellman-Ford algorithm can be used to detect the negative weight loop.
Return to the original question.
Let's talk about my practice during the exam.
Set the minimum short-circuit estimate value of all points to a sufficiently large value, and the minimum short-circuit estimate value of v1 to 0. Then run Bellman-Ford once.
If the figure G contains a negative weight loop, the output is-1;
Otherwise, if the Shortest Path of the vertex labeled N is still a sufficiently large value, the distance between the vertex labeled N and the vertex labeled 1 can be any large, then-2 is output;
If neither of the above conditions is met, the minimum path of the output vertex labeled N is estimated.
What is big enough?
It only needs to be larger than the maximum edge weight x vertex number in the source image.
As long as there is no circle, each vertex can only pass through once at most, so it must be smaller than this value. Therefore, in this graph, we can regard this value as Infinity.
This method can pass all test data of the competition.
During the examination, I did not deliberately prove the correctness of this method, but I felt that it should be feasible.
Now let's prove it theoretically.
Theorem 6 if the minimum short-circuit estimation value of the vertex labeled N is still sufficiently large after Bellman-Ford is run, the distance between N and 1 can be arbitrary.
Proof: As you can see from the operation just now, this step has eliminated the situation that contains a negative weight loop. In Figure G, the full value is greater than the possible maximum distance. Therefore, it has the same effect on G as any big value (also greater than the legal maximum distance ). Because a sufficiently large value satisfies the constraints in G, any large value also satisfies the constraints, so that the distance can be any large. (Certificate completed)
There is another problem.
Theorem 7 if, after running Bellman-Ford, the estimated ratio of the shortest circuit to the vertex labeled N is sufficient, the maximum distance between N and 1 is possible.
Proof: if D [I] is the shortest path of vertex I and 1, d [I] is the maximum possible distance between vertex I and 1.
We first prove that d [n] <= D [n] uses the reverse verification method.
If d [n]> D [n], replace the value given to each vertex I with the corresponding d [I] Before Bellman-Ford runs. Because d itself satisfies all constraints, after running it, D '= d is obtained. Because the full value is larger than all d [I], and the most short-circuit is to use a gradual relaxation operation, we can set up a larger initial value, which cannot lead to a smaller end value. Therefore, for any I, D [I]> = d' [I], that is, D [n]> = D [n], which is in conflict with our assumptions.
Then we prove that d [n]> = D [n].
According to the definition of d [I], it is the maximum possible distance between I and 1. Because D [I] satisfies all the constraints of the question, D [I] is the possible distance between vertex I and 1. If D [I]> d [I], it is in conflict with the definition of d [I. Www.2cto.com
Based on the above, D [I] = d [I]. Thus, D [n] is the maximum possible distance between n and 1. (Certificate completed)
The worst case for running Bellman-Ford is that the complexity is O (ML + MD) * N) = O (2*107), which can be obtained within the specified time. In addition, the actual running speed is quite satisfactory, and most data operations basically do not require time.
Author: zhang20072844