Crazy Circuits
Question:
A circuit board is provided, starting from the + pole to the negative pole. Now, the minimum current limit on the circuit board is required to obtain the minimum current starting from the positive pole when the current is balanced.
Algorithm:
A very bare minimum stream with a source sink. First, find the maximum stream. Then, by adding t --> S capacity INF, it becomes a problem of the minimal stream with no source sink, so that the result is when the maximum stream is run once. Although it does not strictly prove whether the algorithm is correct, I have not found a wrong algorithm.
In [] Forgot to clear the question. It took half an hour !!!
/*//!!!!!!!!!!!!!! Be sure to clear the initialization issue !!!!!!!! */# Include <iostream> # include <algorithm> # include <vector> # include <queue> # include <map> # include <cstdio> # include <cmath> # include <cstring> using namespace STD; const int INF = 1 <25; const int maxn = 100 + 10; /// // struct edge {int from, to, Cap, flow, cost; edge () {}; edge (INT _ from, int _ to, int _ cap, int _ flow): From (_ from ), to (_ to), Cap (_ cap), flow (_ flow) {};}; vector <edge> EDG Es; vector <int> G [maxn]; int cur [maxn], d [maxn]; bool VST [maxn]; int SRC, sink, SS, TT; //////////////////////////////////////// /// int N, m; int low [maxn + maxn], in [maxn]; void Init () {Ss = n + 2; TT = SS + 1; src = tt + 1; sink = SRC + 1; for (INT I = 0; I <= sink; ++ I) g [I]. clear (); edges. clear ();} void GETID (char * str1, char * str2, Int & X, Int & Y) {If (str1 [0] = '+ ') {x = SS;} else if (str1 [0] = '-') {x = tt;} e LSE {sscanf (str1, "% d", & X) ;}if (str2 [0] = '+') {Y = SS ;} else if (str2 [0] = '-') {Y = tt;} else {sscanf (str2, "% d", & Y );}} void addedge (int from, int to, int cap) {edges. push_back (edge (from, to, Cap, 0); edges. push_back (edge (to, from, 0, 0); int SZ = edges. size (); G [from]. push_back (SZ-2); G [to]. push_back (SZ-1);} bool BFS () {memset (VST, 0, sizeof (VST); queue <int> q; q. push (SRC); VST [SRC] = 1; d [SRC] = 0; while (! Q. empty () {int x = Q. front (); q. pop (); For (INT I = 0; I <(INT) g [X]. size (); ++ I) {edge & E = edges [G [x] [I]; If (! VST [E. to] & E. cap> E. flow) {VST [E. to] = 1; d [E. to] = d [x] + 1; q. push (E. to) ;}}return VST [sink];} int DFS (int x, int A) {If (x = sink | A = 0) return; int flow = 0, F; For (Int & I = cur [X]; I <(INT) g [X]. size (); ++ I) {edge & E = edges [G [x] [I]; If (d [E. to] = d [x] + 1 & (F = DFS (E. to, min (A, E. cap-e. flow)> 0) {e. flow + = f; edges [G [x] [I] ^ 1]. flow-= f; flow + = f; A-= f; if (a = 0) break;} retur N flow;} int maxflow () {int flow = 0; while (BFS () {memset (cur, 0, sizeof (cur); flow + = DFS (SRC, INF);} return flow;} int main () {While (scanf ("% d", & N, & M), (N | M )) {Init ();//!!!!!!!!!!!!! Int X, Y; char str1 [10], str2 [10]; memset (in, 0, sizeof (in )); //!!!!!!!!!!!!!!!!!! For (INT I = 0; I <m; ++ I) {scanf ("% S % d", str1, str2, & low [I]); // printf ("% S % s \ n", str1, str2); GETID (str1, str2, x, y ); // printf ("% d --> % d \ n", x, y); addedge (X, Y, INF-low [I]); // has a lower bound, no upper bound in [x]-= low [I]; In [y] + = low [I];} If (n = 0) {printf ("% d \ n", * max_element (low, low + M); continue;} int sum = 0; For (INT I = 1; I <= tt; ++ I) {If (in [I]> 0) {sum + = in [I]; addedge (SRC, I, in [I]);} else {addedge (I, sink ,- In [I]) ;}}sum-= maxflow (); addedge (TT, SS, INF); // converts it to a non-Source Sink Network stream int flow = maxflow (); if (sum! = Flow) {puts ("impossible");} else {int SZ = edges. size (); printf ("% d \ n", edges [SZ-2]. flow) ;}} return 0 ;}
HDU crazy Circuits