The maximum flow of the consumer from the dispatch station, the power station, is given, along with some capacity on the side, and the maximum amount of input and output that the power station and the consumer can enter.
Add a super source point and a meeting point, and build a running template on it. Two templates tease can.
#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath > #include <algorithm> #include <vector> #include <queue> #include <map> #define INF 0x3f3f3f3f#define EPS 1e-6#define ll __int64const int maxn=110;using namespace Std;int n,m,np,nc,s,t;int level[maxn],c[ Maxn][maxn];bool Makelevel () {memset (level,0,sizeof level); Level[s]=1; int Q[MAXN]; int fro=0,iq=0; Q[iq++]=s; int i,v; while (Fro!=iq) {v=q[fro++]; for (i=1;i<=n+2;i++)//Note dot number {if (!level[i]&&c[v][i]) {Level[i]=lev el[v]+1; Q[iq++]=i; }}} if (!level[t]) return 0; return 1;} int dfs (int now,int maxf) {if (now==t) return MAXF; int ret=0; for (int i=1;maxf&&i<=n+2;i++)//Note dot number {if (C[now][i]&&level[now]+1==level[i]) { int Tmp=dfs (I,min (Maxf,c[now][i])); C[now][i]-=tmp; c[i][now]+=tmp; ret+=tmp; maxf-=tmp; }} return ret;} int dinic () {int ans=0; while (Makelevel ()) Ans+=dfs (S,inf); return ans;} /*int C[maxn][maxn],p[maxn];bool BFs () {queue<int> q; BOOL VIS[MAXN]; memset (vis,0,sizeof Vis); memset (p,-1,sizeof p); Q.push (s); Vis[s]=1; while (!q.empty ()) {int E=q.front (); if (e==t) return 1; Q.pop (); for (int i=1;i<=n+2;i++)//Note the range of the point {if (C[e][i]&&!vis[i]) {vis[i]=1; P[i]=e; Q.push (i); }}} return 0;} int ek () {int u,ans=0,mn; while (BFS ()) {mn=inf; u=t; while (p[u]!=-1) {mn=min (mn,c[p[u]][u]); U=p[u]; } ans+=mn; u=t; while (p[u]!=-1) {c[p[u]][u]-=mn; C[U][P[U]]+=MN; U=p[u]; }} return ans; */int Main () {int i,a,b,cc; while (~SCANF ("%d%d%d%d", &n,&np,&nc,&m)) {s=n+1,t=n+2; memset (c,0,sizeof c); for (i=0;i<m;i++) {while (GetChar ()! = ' ('); scanf ("%d,%d)%d", &A,&B,&CC); C[A+1][B+1]=CC; } for (i=0;i<np;i++) {while (GetChar ()! = ' ('); scanf ("%d)%d", &a,&b); C[s][a+1]=b; } for (i=0;i<nc;i++) {while (GetChar ()! = ' ('); scanf ("%d)%d", &a,&b); C[a+1][t]=b; } printf ("%d\n", Dinic ()); } return 0;}