Topic Link: Click to open the link
Test instructions: Given a graph that contains n points, the M-Bar has an edge of 1, and asks if only one edge can be flipped to make the maximum flow of s to t increase to how much? How many ways are there?
Idea: Run the maximum flow first, divide the points into two sets in the residual flow, the first set contains all points that can be reached from the starting point, and the second set contains all points that can reach the end point, then the answer is the number of bars at the end of the second set at the edge of the first set (the points omitted from the two collection)
CPP Code:
#include <cstdio> #include <iostream> #include <cstring> #include <string> #include < algorithm> #include <cmath> #include <map> #include <vector> #include <bitset> #include < queue>using namespace std; #define MAXN 10010#define INF 1e9struct edge{int from,to,cap,flow;}; struct Dinic{int n,m,s,t;vector<edge> edges;vector<int> g[maxn],g[maxn];vector<edge> ed;bool vis[ MAXN]; int d[maxn];int cur[maxn];int un[maxn];void Add (int a,int b) {edges.push_back ((Edge) {a,b,1,0}); Edges.push_back ( (Edge) {b,a,0,0}); M=edges.size (); G[b].push_back (m-2); G[a].push_back (m-1); G[a].push_back (m-2); G[b].push_back (m-1);} BOOL BFs () {memset (vis,0,sizeof (Vis)); Queue<int > Q; Q.push (s);d [S]=0;vis[s]=1;while (! Q.empty ()) {int X=q.front (); Q.pop (); for (int i=0;i<g[x].size (); i++) {edge& e= edges[g[x][i]];if (!vis[e.to]&&e.cap>e.flow) {Vis [E.to]=1;d[e.to]=d[x]+1; Q.push (e.to);}}} return vis[t];} int dfs (int x,int a) {if (x==t| | a==0) return A;int FLow=0,f;for (int& i=cur[x];i<g[x].size (); i++) {edge& e=edges[g[x][i]];if (d[x]+1==d[e.to]&& (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 max_flow (int s,int t) {this->s=s;this->t=t;int flow=0;while (BFS ())) {memset (cur,0,sizeof (cur)); Flow+=dfs (s), INF);} return flow;} void Ddfs (int s,int bj) {if (bj==1) {un[s]=bj;for (int i=0;i<g[s].size (); i++) {if (!un[edges[g[s][i]].to]&& EDGES[G[S][I]].FLOW<EDGES[G[S][I]].CAP) Ddfs (EDGES[G[S][I]].TO,BJ);}} else {un[s]=bj;for (int i=0;i<g[s].size (); i++) {if (!un[edges[g[s][i]].from]&&edges[g[s][i]].flow< EDGES[G[S][I]].CAP) Ddfs (EDGES[G[S][I]].FROM,BJ);}} int solve () {int cnt=0;memset (un,0,sizeof (un));DD FS (s,1);DD FS (t,2); for (int i=0;i<edges.size (); i+=2) {//cout< <edges[i].from<< "<<edges[i].to<<" <<edges[i].flow<< "<<un[edges[i". from]<< "" <<un[edges[i].to]<<endl;if (Un[edges[i].to]==1& &un[edges[i].from]==2&&edges[i].flow<edges[i].cap) cnt++;} return CNT;}}; int main () {//freopen ("data.in", "R", stdin), int n,m,s,t;int ta,tb;while (~scanf ("%d%d%d%d", &n,&m,&s, &t) {dinic m;if (n==0&&m==0&&s==0&&t==0) break;for (int i=0;i<m;i++) {scanf ("%d%d", &TA,&TB); M.add (TA,TB);} int Ans=m.max_flow (s,t); int tp=m.solve (); if (TP) ans++;cout<<ans<< "" <<tp<<endl;} return 0;}
Jag2014autumn F Reverse a Road II network flow