Crazy Circuits
Topic:
A circuit board is given, starting from the + pole to the negative.
The minimum current limit on the circuit board is now given to you to obtain the minimum current from the positive when the current is balanced.
Algorithm:
Very bare with Yuanhui minimum flow. When Ann has the Yuanhui maximum flow method, the maximum flow is calculated first. And then. By adding the T-->s capacity inf, it becomes a non-Yuanhui minimum flow problem. This is the result of running the maximum flow at one time. Although not strictly proven correct, but I use today, have not found the wrong algorithm.
in[when doing a problem] forgot to empty. For half an hour!!!
/* //!!!!!!!!!!!!!! Be sure to pay attention to emptying the initialization problem.!!!!。!。 */#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <map > #include <cstdio> #include <cmath> #include <cstring>using namespace Std;const int INF = 1 << 2 5;const int MAXN = + 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> edges;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; } else {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 A; 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; }} return flow;} int Maxflow () {int flow = 0; while (BFS ()) {memset (cur,0,sizeof (cur)); Flow + = DFS (Src,inf); } return flow;} int main () {while (scanf ("%d%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%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]); There is 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); Convert to no 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