莫名假了,不懂哪错捏。
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10,M=3e4+10,inf=1e9;
struct edge{
int ed,next;
int w;
}a[M*2];
int n,m,tot=1,s,t,ans;
int val[N],nbs[N],p[N],d[N];
int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
bool v[N];
void add(int x,int y,int z){
tot++;
a[tot].ed=y;a[tot].w=z;
a[tot].next=nbs[x];
nbs[x]=tot;
tot++;
a[tot].ed=x;a[tot].w=0;
a[tot].next=nbs[y];
nbs[y]=tot;
return;
}
int loc(int x,int y){
return (x-1)*n+y;
}
void read(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf("%d",&val[loc(i,j)]);
ans+=val[loc(i,j)];
}
return;
}
void init(){
s=0;t=n*m+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if((i+j)&1){
add(s,loc(i,j),val[loc(i,j)]);
for(int k=1;k<=4;k++){
int ex=i+dx[k],ey=j+dy[k];
if(ex>=1&&ex<=n&&ey>=1&&ey<=m)
add(loc(i,j),loc(ex,ey),inf);
}
}else add(loc(i,j),t,val[loc(i,j)]);
return;
}
bool bfs(){
memset(v,0,sizeof(v));
queue<int>q;q.push(s);
d[s]=inf;v[s]=1;
while(!q.empty()){
int k=q.front();q.pop();
for(int x=nbs[k];x;x=a[x].next){
int u=a[x].ed;
if(!a[x].w||v[u])
continue;
p[u]=x;v[u]=1;
d[u]=min(d[k],a[x].w);
q.push(u);
if(u==t)return 1;
}
}
return 0;
}
int EK(){
int res=0;
while(bfs()){
int k=t;
while(k!=s){
int x=p[k];
a[x].w-=d[t];
a[x^1].w+=d[t];
k=a[x^1].ed;
}
res+=d[t];
}
return res;
}
signed main(){
read();init();
printf("%d\n",ans-EK());
return 0;
}