[Question ]:
Give a directed graph (the information is the number of points, the number of sides, the start end point and the weight of each edge), and then let you doAny timeFollow these steps:
Select any node V and a value D to subtract D from the edge weight at the end of V, and add D to the edge weight at the starting point of V,
At last, two conditions must be met: the weight of these edges is non-negative.Edge with the smallest weightThe weight is as large as possible.
[Knowledge point ]:
Bellman-Ford + differential constraint system
[Question ]:
Differential constraint system: It is a method to judge the solution of the inequality group (the specific principle is not quite understood, and will be further supplemented in the future)
For example, if there are several inequalities, such as XJ-xi <= ak, then create a graph using I as the start point j as the end AK as the weight value, and then run it with Bellman-Ford. If we can find the shortest path, the inequality is resolved. Otherwise, no.
Because the operation order does not affect the final result, set sum (a) to the sum of the Operation values on Vertex, the value of a-> B after the operation is complete is W (a, B) + sum (a)-sum (B ).
After a series of operationsEdge with the smallest weight in these edgesIf the value is x, x <= W (a, B) + sum (a)-sum (B) is satisfied for any side, and sum (B)-sum (a) is deformed) <= W (a, B)-X.
First, determine whether + 1 can be used as the edge with the smallest weight value (the value of the maximum edge before the operation is not performed). If feasible, it means that the value of each edge can be infinitely increased through the operation.
Judgment 1. If it is not feasible, it means that no operation can ensure that all edges are non-negative. Then the edge with the smallest binary weight value is obtained.
If the judgment is feasible, the system is restricted by checking the score.
[Code ]:
From Liu lujia's Training Guide
1 // uva11478 halum 2 // rujia Liu 3 # include <cstdio> 4 # include <cstring> 5 # include <queue> 6 using namespace STD; 7 8 const int INF = 1000000000; 9 const int maxn = 500 + 10; 10 const int maxm = 2700 + 10; 11 12 struct edge {13 int to, DIST; 14 }; 15 16 // Writing of the adjacent Table 17 struct bellmanford {18 int n, m; 19 edge edges [maxm]; 20 int head [maxn]; 21 int next [maxm]; 22 bool INQ [maxn]; // whether it is in the queue 23 int d [maxn]; // s to each The distance from the point is 24 int CNT [maxn]; // number of teams entering 25 26 void Init (int n) {27 This-> N = N; 28 m = 0; 29 memset (Head,-1, sizeof (head); 30} 31 32 void addedge (int from, int to, int Dist) {33 next [m] = head [from]; 34 head [from] = m; 35 edges [M ++] = (edge) {to, DIST }; 36} 37 38 bool negativecycle () {39 queue <int> q; 40 memset (INQ, 0, sizeof (INQ); 41 memset (CNT, 0, sizeof (CNT); 42 for (INT I = 0; I <n; I ++) {d [I] = 0; q. push (I );} 43 44 int U; 45 while (! Q. empty () {46 U = Q. front (); q. pop (); 47 INQ [u] = false; 48 for (INT I = head [u]; I! =-1; I = next [I]) {49 edge & E = edges [I]; 50 if (d [E. to]> d [u] + E. dist) {51 d [E. to] = d [u] + E. dist; 52 If (! INQ [E. to]) {q. push (E. to); INQ [E. to] = true; If (++ CNT [E. to]> N) return true;} 53} 54} 55} 56 return false; 57} 58}; 59 60 bellmanford solver; 61 62 // determine whether 63 bool test (int x) {64 for (INT I = 0; I <solver. m; I ++) 65 solver. edges [I]. dist-= x; 66 bool ret = solver. negativecycle (); 67 for (INT I = 0; I <solver. m; I ++) 68 solver. edges [I]. dist + = x; 69 return! RET; // if there is a negative ring, the difference constraint system has no solution 70} 71 72 int main () {73 int n, m; 74 while (scanf ("% d", & N, & M) = 2) {75 solver. init (n); 76 int UB = 0; 77 while (M --) {78 int U, V, D; 79 scanf ("% d ", & U, & V, & D); UB = max (UB, d); 80 solver. addedge (U-1, V-1, d); 81} 82 83 If (test (UB + 1) printf ("infinite \ n "); // if you can set each edge to be greater than UB, it means that the right of each edge is increased and repeated operations will increase... until 84 else if (! Test (1) printf ("no solution \ n"); 85 else {86 int L = 2, R = UB, ANS = 1; 87 while (L <= r) {88 int M = L + (R-L)/2; 89 If (test (M) {ans = m; L = m + 1;} else r = M-1; 90} 91 printf ("% d \ n", ANS); 92} 93} 94 return 0; 95}