Zoj–2770burn the Linked Camp single source Shortest path differential constraint system

Source: Internet
Author: User

ZOJ problem set–2770 Burn the Linked Camp

The main idea of the topic is:

Lu Xun investigation learned that Liu Bei will own the army into N Battalion, from left to right numbered for three-in-one ... N. The first Battalion has the largest number of soldiers CI, through a period of observation, Lu Xun can be estimated to a few K soldiers live in the first I to the J camp, and finally Lu Xun must estimate Liu Bei's army at least how many soldiers, in order to deal with.

Input:

There are multiple test cases, in the first line of each test case, there are two integers n (0

Output:

For each test case, an integer in one line, output: estimate the minimum number of soldiers in the Liu Bei Army according to Lu Xun's observations. However, Lu Xun estimates may be inaccurate in the input data. If his estimate is untrue, output "bad estimations" in a single line.

Problem Solving Ideas:

The title asks for the minimum value of the soldiers in Liu Bei's army from the observation results. Since each camp has the maximum number of soldiers to accommodate, there are unequal constraints, and the problem can be considered with a differential constraint system.

Differential constraint system:

If a system consists of n variables and m constraints, each of these constraints is shaped like this:

XJ-XI<=BK (I,j∈[1,n],k∈[1,m])

It is called a differential constraint system .

Solving differential Constraint systems:

The solution of differential constrained system can be transformed into a single-source shortest path problem of graph theory,

Xj-xi<=bk

Xj<=xi+bk

Triangular inequalities similar to shortest path

distance[v]<= Distance[u]+weight[u,v]

IE Distance[v]-distance[u]<=weight[u,v]

Therefore, with each variable Xi as the node, for the constraint condition

XJ-XI<=BK is XJ<=XI+BK

Connect an edge (i,j) with a weight of BK

Add a source point a,a and all vertices are connected, the weights are 0, if there is a negative ring in the diagram, there is no solution, otherwise, from the source point to the other vertex of the single source shortest path is the problem of a set of feasible solutions.

There are n campsites, numbered I (I=1, 2, 3 ...). N), K (i) represents section I (I=1, 2, 3 ... N) The number of soldiers in a camp plus the number of soldiers in all camps with a number less than I.

Then section J (J=1, 2, 3 ... N) The number of soldiers in a camp is K (j)-K (j-1)

If M (j) indicates the maximum number of soldiers the camp J can hold, there is an unequal relationship:

K (j)-K (J-1) <=m (j)

Transformed to

K (j) <=k (J-1) +m (j)

To avoid the special circumstances of the first camp, we added a camp with a number of 0 and 0 soldiers, K (1)-K (0) =k (1). The camp was only a supplement, and had no effect on the results.

We set the camp for each number I as vertex i (i=0,1,2 ... N), we add a source point, the source point to all other vertices have a forward edge, the weight of 0, do not need to save the point, just auxiliary solution, to ensure the connectivity of the graph. K (j) and K (j-1) are the shortest path weights from the source to the j,j-1 two points, and if the number of soldiers in Camp J is M (j), there is K (j) <=k (J-1) +m (j), which can be connected from J to j-1 with a forward edge with a weight of M (j), The problem is converted to a single source shortest path from the source point to the vertex J.

Because the shortest path from source to point J must satisfy K (j) <=k (J-1) +m (j)

Similarly, at least X soldiers from camp I to J have unequal relationships:

K (j)-K (i-1) >=x

Transform to

K (I-1) <=k (j)-X

Thus, from the vertex j to the vertex i-1 a forward edge, the weight is-X, to solve the shortest path from the source point to J Point must also satisfy the inequality K (i-1) <=k (j)-X

where K (1), K (2) ... K (N) is a set of solutions for a differential constrained system, and when the answer is non-negative, we add a constant c to each solution.

The shortest path to the last source-to-point n is the solution, but in fact the number of soldiers cannot be negative, because the minimum number of soldiers we can add a minimum constant C, so that the group solution is not negative.

There is no solution to the problem when there is a negative weight ring in the diagram. Because if there is a negative weight ring, the shortest path tends to be negative infinity, and the shortest path is obviously not possible.

The problem involves negative weights, and it is more suitable to solve the shortest path with Bellman Ford algorithm.

Bellman Ford algorithm idea is based on the following facts:

"If there is a shortest path between two points, then each node is at most once." ”

That is, for the graph of N nodes, this road does not exceed the n-1 edge.

If a node goes through two, we walk a circle:

If the right of the circle is positive, it is obviously not cost-effective;

If it is a negative circle, there is no shortest path;

If it is 0 laps, remove does not affect the shortest path.

The shortest path with the upper limit of the number of paths is M, which can be obtained by the shortest path "plus an edge" when the upper limit of the number of edges is m-1, and the shortest path can be found at most only by iterating n-1 times.

Bellman Ford algorithm pseudo-code is as follows:

V (g) is the vertex set of Figure g, and E (g) is the edge set of Figure g

For i=0 to | V (G) |

For each side (U,V) belongs to E (G)//slack each side

if (Distance[v]>distance[u]+weight[u,v])

DISTANCE[V]=DISTANCE[U]+WEIGHT[U,V]

End for

End for

For each edge (U,V) belongs to E (G)//check for negative loops

if (Distance[v]>distance[u]+weight[u,v])

return False

End for

return True

Algorithm time complexity O (VE).

The algorithm is simple and easy to understand, the disadvantage is that the time complexity is high, here can be optimized with the queue, the focus of this paper is how to apply the shortest path algorithm in the differential constraint system, will not repeat.

The Bellman Ford algorithm uses relaxation techniques:

To maintain the distance from one source to all other points table distance[], the process of relaxing an edge (U,V) includes testing whether we can modify the shortest path to the current to v node by node U distance[v], and update distance[v if the path through U reaches v is shorter! ]。 This is to ensure that every two nodes have

distance[v]<= Distance[u]+weight[u,v]

The pseudo code is as follows:

if (distance[v]> distance[u]+weight[u,v])//non-conforming on the adjustment

DISTANCE[V] = Distance[u]+weight[u,v]

Problem Solving Code:

1#include <stdio.h>2 3#include <vector>4 5 int*dislist;//distance table maintained when finding the shortest path6 7 structedge{//Edge Structure8 9        intStart//starting pointTen  One        intEnd//End A  -        intWeight//Weighted Value -  the }; -  -Std::vector<edge> E;//container for storing edge sets -  +Std::vector<edge>::iterator it;//iterators -  + BOOLBellmanford (intN//find the shortest path to the source point for each vertex A  at { -  -   -  -         while(--n)//take the N-1 side of the road -  in        { -  to                for(it = E.begin (); It! = E.end (); it++)//to relax each side of the line +  -               { the  *                      if(It->weight + dislist[it->start]< dislist[it->end]) $ Panax Notoginseng                      { -  theDislist[it->end] = it->weight + dislist[it->start]; +  A                      } the  +               } -  $        } $  -         for(it = E.begin (); It! = E.end (); it++)//If it can also be relaxed, indicating the existence of a negative ring, no solution -  the        { - Wuyi               if(It->weight + dislist[it->start]< dislist[it->end]) the  -               { Wu  -                      return false; About  $               } -  -        } -  A        return true; +  the }; -  $ voidOutputintN) the  the { the  the        if(!bellmanford (N)) printf ("Bad estimations\n"); -  in        Else//according to the actual situation, the barracks soldier number can not be negative, will all be converted to positive the  the        { About  the               intMin = dislist[0]; the  the                for(inti =1; I <= N; i++) +  -               { the Bayi                      if(Min > dislist[i]) min =Dislist[i]; the  the               } -  -               if(Min <0) the  the               { the  theDislist[n] = Dislist[n]-min; -  the               } the  theprintf"%d\n", Dislist[n]);94  the        } the  the };98  About intMain () - 101 {102 103        intN, M;104  the         while(scanf_s ("%d%d", &n,&m) = =2)106 107        {108 109E.clear ();//Empty Container the 111Dislist =New int[N +1]; the 113                for(inti =0; I <= N; i++)//Initialize distance table the  the               { the 117Dislist[i] =0;118 119               } - 121               //input Data122 123 Edge E;124  the                for(inti =1; I <= N; i++)126 127               { - 129scanf_s ("%d", &e.weight); the 131E.start = i-1; the 133E.end =i;134 135E.push_back (e);//The maximum number of soldiers in Camp I is w, which is inserted into the matrix by the i-1 pointing to the I-weighted value W of the forward side136 137               }138 139               intA, b,c; $ 141                for(inti =0; i < M; i++)142 143               {144 145scanf_s ("%d%d%d", &a,&b,&c);146 147E.start =b;148 149E.end = A-1; Max 151E.weight =-C; the 153E.push_back (e);//at least the number of soldiers between Camp A and Camp B is C, and a directed edge is inserted from point B to point A in the matrix diagram with the weight value C.154 155               }     156 157Output (N);//Output Results158 159               Delete[]dislist;//Freeing Memory the 161        }162 163 GetChar ();164 165        return 0;166 167}

Zoj–2770burn the Linked Camp single source Shortest path differential constraint system

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.