翻转源汇比较容易写错。。。。要注意
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define maxn 55 #define maxv 10050 #define maxe 2000050 #define inf 1000000007 using namespace std; int n,m,a[maxn][maxn],b[maxn][maxn],id[maxn][maxn][3],tot=0; int s,t,dis[maxv],nume=1,ans=0,g[maxv]; queue <int> q; struct edge { int v,f,nxt; }e[maxe]; void addedge(int u,int v,int f) { e[++nume].v=v;e[nume].f=f;e[nume].nxt=g[u];g[u]=nume; e[++nume].v=u;e[nume].f=0;e[nume].nxt=g[v];g[v]=nume; } void build() { s=0;t=++tot; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { if (!((i+j)&1)) { addedge(s,id[i][j][1],a[i][j]); addedge(id[i][j][1],t,b[i][j]);addedge(s,id[i][j][2],b[i][j]); addedge(id[i][j][2],id[i][j][1],inf); if (i>1) addedge(id[i][j][2],id[i-1][j][1],inf); if (i<n) addedge(id[i][j][2],id[i+1][j][1],inf); if (j>1) addedge(id[i][j][2],id[i][j-1][1],inf); if (j<m) addedge(id[i][j][2],id[i][j+1][1],inf); } else { addedge(id[i][j][1],t,a[i][j]); addedge(s,id[i][j][1],b[i][j]);addedge(id[i][j][2],t,b[i][j]); addedge(id[i][j][1],id[i][j][2],inf); if (i>1) addedge(id[i-1][j][1],id[i][j][2],inf); if (i<n) addedge(id[i+1][j][1],id[i][j][2],inf); if (j>1) addedge(id[i][j-1][1],id[i][j][2],inf); if (j<m) addedge(id[i][j+1][1],id[i][j][2],inf); } } } bool bfs() { for (int i=s;i<=t;i++) dis[i]=inf;dis[s]=0; q.push(s); while (!q.empty()) { int head=q.front();q.pop(); for (int i=g[head];i;i=e[i].nxt) { int v=e[i].v; if (dis[v]>dis[head]+1 && e[i].f>0) { dis[v]=dis[head]+1; q.push(v); } } } return dis[t]!=inf; } int dinic(int x,int low) { if (x==t) return low; int ret=0; for (int i=g[x];i && low;i=e[i].nxt) { int v=e[i].v; if (e[i].f>0 && dis[v]==dis[x]+1) { int dd=dinic(v,min(low,e[i].f)); ret+=dd;low-=dd;e[i].f-=dd;e[i^1].f+=dd; } } if (!ret) dis[x]=inf; return ret; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) scanf("%d",&a[i][j]); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { scanf("%d",&b[i][j]);ans+=2*b[i][j]; id[i][j][1]=++tot; } for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) id[i][j][2]=++tot; build(); while (bfs()) ans-=dinic(s,inf); printf("%d\n",ans); return 0; }