UVa 515-king (Differential constrained system + SPFA with negative right shortest)

Source: Internet
Author: User

Here is a detailed description of the differential constraint system, and the solution ~ excerpt from Xuezhongfenfei (he seems to be rotating ...). )

Differential constraint system

X1-X2 <= 0
X1-X5 <=-1
X2-X5 <= 1
X3-X1 <= 5
X4-X1 <= 4
X4-x3 <=-1
X5-X3 <=-3
X5-x4 <=-3
Inequalities Group (1)

It's all two unknowns. The difference is less than or equal to a constant (greater than or equal to, since left and right multiplied by-1 can be converted to less than or equal). Such an inequality group is called a differential constrained system.

This inequality group is either no solution, or there are countless groups of solutions. Because if there is a set of solutions {X1, X2, ..., Xn} then for any one of the constants K,{x1 + K, X2 + K, ..., Xn + k} must also be a set of solutions, since any two number is added to a number at the same time and their difference is constant, then all inequalities in this differential constrained system will not Be destroyed.  

The solution of the differential constraint system utilizes the triangular inequalities in the single source shortest path problem. That is, for any one edge U-V, there are:
d (v) <= D (U) + w (u, v)
   where D (U) and D (v) are the weights of the shortest path from the source point to the U and Point v respectively, W (U, v) is the weight of the edge U-V. obviously the above inequalities are D (v)-D (U) <= w (u, v). This form is exactly the same as the inequality form in the differential constrained system.so we can transform a differential constraint system into a graph, each unknown XI corresponds to a vertex vi in the graph, and all inequalities are turned into an edge in the graph. For inequality Xi-xj <= C, it is turned into a triangular inequality: Xi <= Xj + C, you can turn to the edge VJ--Vi, the weight of c. Finally, we find a single-source shortest path on this graph, and these triangular inequalities will all be satisfied, because it is the basic nature of the shortest path problem.
In other words, the so-called single-source shortest path, of course, to have a source point, and then seek this source point to all other points of the shortest path. Sowhere is the source point? We might as well have made one ourselves. Taking the above inequality group as an example, we add an unknown X0. And then to the original each unknown to X0 arbitrarily add an inequality (this inequality of course and other inequalities form the same, that is, the difference of two unknowns is less than or equal to a certain constant). We simply wrote xn-x0 <= 0, so the following inequalities are found in this differential constraint system:
x1-x0 <= 0
x2-x0 <= 0
x3-x0 <= 0
x4-x0 <= 0
x5-x0 <= 0
Inequalities Group (2)
For these 5 inequalities, the corresponding edges are also built in the diagram. The resulting figure is as follows:
Figure 1

Each edge in the graph represents an inequality in the differential constrained system. Now take V0 as the source point, and find the shortest path of single source.The shortest path length obtained from V0 to VN is a solution to the xn.。 As you can see from Figure 1, this set of solutions is {-5,-3, 0,-1,-4}. Of course, adding 10 to each number is also a set of solutions: {5, 7, 10, 9, 6}. But this set of solutions satisfies only the inequality group (1), which is the original differential constraint system, and does not satisfy the inequality group (2), which is the inequalities we add later. Of course this is irrelevant, because X0 is an outsider, we added to the later, full of not satisfied with the inequality of X0 we do not care.
There is also the possibility that there will be no solution, that is, there is no shortest path from the source point to a vertex. Also said that there is a circle of negative power in the picture。 I will not unfold this point, please see some basic theorems of the shortest path problem.
In fact, for Figure 1, it represents a set of solutions that are {0,-5,-3, 0, 1, 4}, meaning that the value of X0 is also in this set of solutions. But the value of X0 is undisputed, since it is the shortest path to the source, the shortest path length of the source point is of course 0. Therefore, in fact, we solve this differential constraint system virtually there is a condition:
X0 = 0
That is, in the inequality group (1), (2) composed of a differential constraint system, and then the value of one of the unknowns is determined to die. Such a situation is very common in practical problems. For example, a problem on the surface of some inequalities, but also hidden some inequalities, such as all unknowns are greater than or equal to 0, or not more than a certain limit. For example, the above inequality group (2) specifies that all unknowns are less than or equal to 0.
for this differential constrained system with an unknown value, there is also an interesting property, that is, the shortest path algorithm to find out a set of solutions, all unknowns are reached maximum. Let me give you a rough proof that this process of proving is to be illustrated by the process of combining the Bellman-ford algorithm.
Suppose X0 is dead; X1 to xn the maximum value that can be taken in cases where all constraints are met is M1, M2 、......、 Mn (of course we do not know what their values are); The shortest path length of the extracted source points to each point is D1, D2 、......、 Dn.
The basic Bellman-ford algorithm is the initial initialization of the D1 to the DN, which are infinitely large. Then we check all the edges corresponding to the triangle inequality, one but found that there is not satisfied with the triangle inequality, then update the corresponding D value. The last D1 to the DN is the shortest path length from the source point to each point.
If we initially initialized the values of D1, D2 、......、 dn to M1, M2 、......、 Mn respectively, then because they all satisfy the triangular inequalities (we have just assumed that M1 to Mn is a valid set of solutions), then the Bellman-ford algorithm will no longer be new to the D value, The final Solution is M1, M2 、......、 Mn.
Well, now you know, when the initial value is infinite, the D1, D2 、......、 Dn are calculated, and when the initial value is smaller, the M1, M2 、......、 Mn are calculated. We use the same algorithm, the same calculation process, it is not possible to calculate the initial value of a large number of results but small bar. So D1, D2 、......、 DN is M1, M2 、......、 Mn.
What if the minimum value of all other unknowns is required in the case of a dead unknown? As long as the longest path in reverse is possible. The trigonometric inequalities in the longest path are the opposite of the shortest path:
D (v) >= D (U) + w (U, v)
i.e. d (v)-D (U) >= w (u, v)
So when you build the map, you have to put all the inequalities into greater than equal numbers.。 Other processes, including proofs of why the minimum value was solved, are quite similar.

In recent days the system has learned some of the principles of differential constraint systems, which are hereby recorded as follows:
The so-called differential constraint system, refers to a set of indefinite equations (A,X,T,B), where a of each row has a 1, a-1, the rest is 0,x for the solution vector, T is a vector of <= or >=, B is a constrained vector. Specifically, each line has the form of Xi-xj >=|<= bi. The goal of the constraint is to make the target function Xt-xs maximum or minimum.
This is a typical case of linear programming, but can also be converted to graph theory, using the shortest path (or the longest road) method can be achieved efficient solutions.


Introduction of SPFA:

http://blog.csdn.net/u013382399/article/details/44227003

This concludes with your own code:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < queue>using namespace Std;const int maxn = 110;const int INF = 0x3f3f3f3f;struct edge{int u,v,w,next;} Edge[maxn*maxn];int d[maxn],in[maxn],next[maxn];bool vis[maxn];int tot;int n,m;queue<int> q;void AddEdge (int u,    int V,int w) {edge[tot].u = u;    EDGE[TOT].V = v;    EDGE[TOT].W = W;    Edge[tot].next = Next[u]; Next[u] = tot++;}    BOOL SPFA (int size) {int u,v;    while (!q.empty ()) Q.pop ();    memset (In,0,sizeof (in));    memset (vis,0,sizeof (VIS));    Fill (D,d+size,inf);    D[N+1] = 0;vis[n+1] = true;in[n+1] = 1;    Q.push (n+1);        while (!q.empty ()) {u = Q.front (); vis[u] = false; Q.pop ();            for (int i = next[u]; I! =-1; i = edge[i].next) {v = edge[i].v;                if (D[u] + EDGE[I].W < D[v]) {D[v] = D[u] + edge[i].w;                    if (!vis[v]) {Vis[v] = true;                   IN[V] + +; if (In[v] > size) return false;                Q.push (v); }}}} return true;    int main () {int si,ni,ki;    Char oi[5];        while (scanf ("%d", &n)) {if (n = = 0) break;        scanf ("%d", &m);        memset (Next,-1,sizeof (next));        tot = 0; Create yourself a super origin, establish a link with other points (make it connected to other points)////Because SPFA has no way to handle the disconnected diagram for (int i = 0; I <= N; i++) {Addedge ( n+1,i,0)/////////////////////I make point n+1 as a super source point, the Benquan of the remaining nodes is 0} for (int i = 1; I <= m; i++) {scanf ("%d%d%s%d", &si,            &ni,oi,&ki);            if (oi[0] = = ' G ') Addedge (si+ni,si-1,-(ki+1));        if (oi[0] = = ' L ') Addedge (si-1,si+ni,ki-1);    } if (SPFA (n + 2)) printf ("Lamentable kingdom\n"),//////////("successful conspiracy\n") for a total of n+2 point else printf; } return 0;}

If you have any questions, please leave a message, hope that the great God passed by ~ Liberal Enlighten





UVa 515-king (Differential constrained system + SPFA with negative right shortest)

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.