Click to open link
Multi-source multi-sink maximum flow, virtual One source point s ' and a meeting point T ', the original source point, the sink point to their edge.
#include <cstdiO> #include <cstring> #include <iostream> #include <algorithm> #include < Queue> #include <vector>using namespace Std;const int maxn = + 5;const int INF = 100000000;struct edge{int From, to, cap, flow;}; struct dinic{int n, m, S, t; Vector<edge> edges; Vector<int> G[MAXN]; BOOL VIS[MAXN]; int D[MAXN]; int CUR[MAXN]; void init (int n) {this->n = n; for (int i=0; i<=n; ++i) g[i].clear (); Edges.clear (); } void Addedge (int from, int to, int caps) {Edges.push_back (Edge) {from, to, Cap, 0}); Edges.push_back (Edge) {To, from, 0, 0}); m = Edges.size (); G[from].push_back (m-2); G[to].push_back (m-1); } bool BFS () {memset (Vis, 0, sizeof vis); Queue<int> Q; Q.push (s); Vis[s] = 1; D[s] = 0; 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 Maxflow (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; }};D inic Solver;int main () {int n, NP, NC, M; Char ch; int x, y, Z; while (~SCANF ("%d%d%d%d", &n, &NP, &NC, &m)) {int s = 0, t = n+1; Solver.init (t+2); for (int i=1; i<=m; ++i) {cin>>ch>>x>>ch>>y>>ch>>z; if (x==y) continue; Solver. Addedge (x+1, y+1, z); } for (int i=1; i<=np; ++i) {cin>>ch>>x>>ch>>z; Solver. Addedge (S, x+1, z); } for (int i=1; i<=nc; ++i) {cin>>ch>>x>>ch>>z; Solver. Addedge (x+1, T, z); } int ans = solver. Maxflow (s,t); printf ("%d\n", ans); } return 0;}