Title: Hdoj 3157 Crazy Circuits
Test instructions: Now we're going to make a circuit board. There are n electronic components on the circuit board, and there is a unidirectional current flow between the components. Then there is a +. Current enters,--the current sinks in, and then infers if the circuit board can work, assuming the minimum current is possible.
Analysis: The upper and lower bounds of the network flow, to find the minimum flow
The first is to infer that the circuit board can not work. The condition of working is the flow balance. The inference method is discussed in the previous topic.
same as first conversion to no sink network flow problem, join t → s edge right is infinite.
So the minimum flow is not the flow that satisfies the whole nether.
As mentioned above, after the maximum flow of SS→ TT is obtained, the Benquan of its remorse edge s→ t is the minimum flow. But wa . Here's a sample of wa :
Finally, the SS is obtained → after the maximum flow of TT, get the remorse edge s → T is Benquan , in fact, the network minimum flow only to :
s → 1:100
1 → 3:200
3 → 2:200
2 → 1:100
2 → t:100
The problem is that there is a ring in the original. Loop flow, and we're not using it, causing the flow to increase.
Workaround: Don't add t first → s edge right for infinite side, ask SS → The maximum flow of the TT, assuming that there is no flow full then add t → s edge right for infinite edge, and then the maximum flow to get the remorse edge s → T is the minimum flow of the original problem.
Ps:
1: The edges in the network stream are forward. Be sure to pay attention to the difference, and have the upper and lower bounds of the network linger Super Super Source Point SS and Super Super Point TT built edge. Current point traffic come-to < 0, which is required to enter traffic. Built-in s----> I, when cone-to > 0, need to go out of traffic, build the edge I---> TT. But that's not the problem. Just the opposite. So it took a long time to start thinking clearly.
#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 the hierarchy diagram int cur[n];void add_node (int from,int to,int cap) {E.push_back ((Node) {from,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 guarantees {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)//optimize return F; int a = 0,ans=0; for (int &i=cur[o];i<v[o].size (); i++)//Note front ' & ', very 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; Ans+=a of the mode of storage and mapping; F-=a; if (f==0)//attention optimization break; }} return ans; Optimized}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],len=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 * + (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%d%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%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%d%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 "with nether minimum Flow"