Title: hdoj 3157 crazy Circuits
Now we want to make a circuit board. There are n electronic components on the circuit board, and there is a unidirectional flow of current between each component. Then there is a plus, the current enters, and the current is imported, then judge whether the circuit board can work. If yes, find the minimum current.
Analysis: the upper and lower bound network streams are used to find the smallest streams.
The first is to determine whether the circuit board can work, and the condition for the operation is the traffic balance. The judgment method is described in the previous topic.
In the same way, it is first converted to a network flow problem with no source sink, and the t → s edge weight is infinite. Isn't the minimum stream a stream that satisfies all lower bounds. As mentioned above, after obtaining the largest stream of SS → TT, The Edge Weight of the regret edge S → T is the smallest stream. But wa, let's look at an example of WA:
After obtaining the maximum stream of SS → TT, The Edge Weight of the regret edge S → T is 200. In fact, the minimum stream of the network is 100:
S →
1 →
3 →
2 →
2 → T: 100
The problem lies in the presence of loops and loops in the source image, which we did not use, resulting in a larger stream.
Solution: do not add the t → s edge weight to the infinite edge first, and find the maximum stream of SS → TT. If the stream is not full, add the t → s edge weight to the infinite edge, find the biggest stream again to get the regret edge S → T is the smallest stream of the original problem.
PS:
1, the current traffic come-to <0, that is, when the traffic needs to enter, the edge s ----> I. When the cone-to> 0, the traffic needs to go out, edge creation I ---> TT. However, this question is not the case. It is just the opposite. So after a long time, I didn't think clearly at first.
# Include <cstdio> # include <cstring> # include <string> # include <iostream> # include <algorithm> # include <vector> # include <map> # include <queue> # define del (, b) memset (a, B, sizeof (A) using namespace STD; const int INF = 0x3f3f3f3f; const int n = 150; struct node {int from, to, cap, flow ;}; vector <int> V [N]; vector <node> E; int vis [N]; // build a layered graph int cur [N]; void add_node (int from, int to, int cap) {e. push_back (node) {fro M, to, Cap, 0}); E. push_back (node) {to, from, 0, 0}); int TMP = E. size (); V [from]. push_back (tmp-2); V [to]. push_back (tmp-1);} bool BFS (int s, int t) {del (VIS,-1); queue <int> q; q. push (s); vis [s] = 0; while (! Q. empty () {int x = Q. front (); q. pop (); For (INT I = 0; I <V [X]. size (); I ++) {node TMP = E [V [x] [I]; If (vis [TMP. to] <0 & TMP. cap> TMP. flow) // The second condition ensures {vis [TMP. to] = vis [x] + 1; q. push (TMP. to) ;}}} if (vis [T]> 0) return true; return false;} int DFS (int o, int F, int T) {If (O = T | f = 0) // returns F; int A = 0, ANS = 0; For (Int & I = cur [O]; I <V [O]. size (); I ++) // note the preceding '&', which is an important optimization {node & TMP = E [V [O] [I]; if (vis [TMP. to] = (Vis [O] + 1) & (A = DFS (TMP. to, min (F, TMP. cap-tmp.flow), t)> 0) {TMP. flow + = A; E [V [O] [I] ^ 1]. flow-= A; // save graph mode ans + = A; F-= A; If (F = 0) // note break optimization;} return ans; // optimization} int dinci (int s, int t) {int ans = 0; while (BFS (S, T) {del (cur, 0 ); int TM = DFS (S, INF, T); ans + = TM;} return ans;} void mp_clear (int n) {for (INT I = 0; I <= N; I ++) V [I]. clear (); E. clear () ;}int in [N]; int solve (string S, int N, int ff) {int ans [5], le N = 0, TMP = 0; For (INT I = 0; I <S. size (); I ++) {If (s [I] = '+') ans [Len ++] = 0, TMP = 0, I ++; else if (s [I] = '-') ans [Len ++] = n + 1, TMP = 0, I ++; else if (s [I] = '') ans [Len ++] = TMP, TMP = 0; else TMP = TMP * 10 + (s [I]-'0');} ans [Len ++] = TMP; add_node (ANS [0], ANS [1], INF); in [ans [0]-= ans [2]; In [ans [1] + = ans [2]; // printf ("% d \ n", ANS [0], ANS [1], ANS [2]); if (ANS [0] = 0 & Ans [1] = (n + 1) | ans [1] = 0 & Ans [0] = (n + 1) {FF + = ans [2];} return ff;} int main () {// freopen ("input.txt", "r", stdin); int n, m; while (~ Scanf ("% d", & N, & M) & M + n) {getchar (); // del (in, 0); int S = 0, t = n + 1, FF = 0; For (INT I = 0; I <m; I ++) {string s; Getline (CIN, S ); FF = solve (S, N, ff);} int Ss = t + 1, TT = SS + 1; int sum = 0; For (INT I = 0; I <= T; I ++) {If (in [I]> 0) sum + = (in [I]), add_node (SS, I, in [I]); If (in [I] <0) add_node (I, TT,-in [I]);} int ans2 = dinci (SS, TT ); add_node (t, s, INF); int ans1 = dinci (SS, TT); int ans = ans2 + ans1; // printf ("% d \ n", ans2, ans1, sum); If (ANS = sum) printf ("% d \ n ", E [E. size ()-2]. flow); else puts ("impossible"); mp_clear (TT);} return 0 ;}
Hdoj 3157 crazy circuits [minimum stream with lower bound]