1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include<bits/stdc++.h> using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f; const int MAXN=100005;
int N,M,S,T; ll sum;
int nx[]={0,1,0,-1}; int ny[]={1,0,-1,0};
struct Edge{ int v,w,nxt; }mem[MAXN]; int head[MAXN],cnt=1;
inline void AddEdge(int u,int v,int w){ mem[++cnt].w=w; mem[cnt].v=v; mem[cnt].nxt=head[u]; head[u]=cnt; }
int d[MAXN]; bool vis[MAXN];
inline bool bfs(){ memset(vis,0,sizeof(vis)); vis[S]=1;d[S]=0; queue<int> q; q.push(S); while(q.size()){ int u=q.front();q.pop(); for(register int i=head[u];i;i=mem[i].nxt){ int v=mem[i].v,w=mem[i].w; if(vis[v]||(w==0)) continue; vis[v]=1;d[v]=d[u]+1; q.push(v); } } return vis[T]; }
inline int dfs(int u,int flow){ if(u==T) return flow; int rflow; for(register int i=head[u];i;i=mem[i].nxt){ int v=mem[i].v,w=mem[i].w; if(w==0||d[u]+1!=d[v]) continue; if(rflow=dfs(v,min(flow,w))){ mem[i].w-=rflow; mem[i^1].w+=rflow; return rflow; } } return 0; }
inline int Dinic(){ int maxflow=0,lowflow; while(bfs()){ while(lowflow=dfs(S,INF)) maxflow+=lowflow; } return maxflow; }
int main(){ scanf("%d%d",&M,&N); S=0,T=M*N+1; for(register int i=1;i<=M;++i){ for(register int j=1;j<=N;++j){ int w; scanf("%d",&w); sum+=w; if((i+j)&1){ AddEdge(S,(i-1)*N+j,w); AddEdge((i-1)*N+j,S,INF); for(register int k=0;k<=3;++k){ int tx=i+nx[k],ty=j+ny[k]; if(tx<1||tx>M||ty<1||ty>N) continue; AddEdge((i-1)*N+j,(tx-1)*N+ty,INF); AddEdge((tx-1)*N+ty,(i-1)*N+j,0); } } else{ AddEdge((i-1)*N+j,T,w); AddEdge(T,(i-1)*N+j,INF); } } } printf("%lld",sum-Dinic()); return 0; }
|