http://blog.csdn.net/lxy767087094/article/details/68942422
#include <cstdio> #include <cstring> #include <algorithm> #include <queue>using namespace std; const int Dx[]={0,-1,0,1},dy[]={-1,0,1,0};typedef Long long ll;const ll INF=10000000000000000LL; #define MAXN 2505# Define MAXM 30005int v[maxm],en,first[maxn],next[maxm];ll cap[maxm];int d[maxn],cur[maxn];queue<int>q;int S,T; void Init_dinic () {memset (first,-1,sizeof (first)); en=0;} void Addedge (const int &u,const int &v,const ll &w) {v[en]=v; cap[en]=w; Next[en]=first[u]; first[u]=en++; V[en]=u; cap[en]=0; NEXT[EN]=FIRST[V]; first[v]=en++;} BOOL BFs () {memset (d,-1,sizeof (d)); Q.push (S); d[s]=0; while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=first[u];i!=-1;i=next[i]) if (d[v[i]]==-1 && cap[i]) {D[v[i]]=d[u ]+1; Q.push (V[i]); }} return d[t]!=-1;} ll DFS (int u,ll a) {if (u==t | |!a) return A; ll Flow=0,f; for (int &i=cur[u];i!=-1;i=next[i]) if (D[u]+1==d[v[i]] && (F=dfs (V[i],min (a,cap[i)))) {cap[i]-=f; cap[i^1]+=f; Flow+=f; A-=f; if (!a) break; } if (! Flow) D[u]=-1; return Flow;} ll Max_flow () {ll flow=0,tmp=0; while (BFS ()) {memcpy (cur,first,sizeof (first)); while (Tmp=dfs (S,inf)) flow+=tmp; } return Flow;} int N,m,pen,a,b,id[55][55];char A[55][55];int Main () {scanf ("%d%d%d%d", &n,&m,&a,&b); for (int i=1;i <=n;++i) {for (int j=1;j<=m;++j) {id[i][j]=++pen;}} for (int i=1;i<=n;++i) {scanf ("%s", a[i]+1);} s=pen+1; T=s+1;init_dinic (); for (int. i=1;i<=n;++i) {for (int j=1;j<=m;++j) {if (a[i][j]== '. ') {Addedge (S,id[i][j], (LL) B);} Else{addedge (Id[i][j],t, (ll) B);}}} for (int i=1;i<=n;++i) {for (int. j=1;j<=m;++j) {for (int k=0;k<4;++k) {int tx=i+dx[k],ty=j+dy[k];if (tx>=1 && tx<=n && ty>=1 && ty<=m) {Addedge (Id[i][j],id[tx][ty], (LL) A);}}} printf ("%lld\n", Max_flow ()); return 0;}
min cut "dinic" gym-101128f-landscaping