和小M的作物类似建图跑最小割即可(这题的建图实在令人郁闷。。
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(int i=l;i<=r;i++) 3 #define dec(i,l,r) for(int i=l;i>=r;i--) 4 #define link(x) for(edge *j=h[x];j;j=j->next) 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define inf 1e9 7 #define ll long long 8 #define succ(x) (1<<x) 9 #define NM 70000+5 10 #define nm 500000 11 using namespace std; 12 int read(){ 13 int x=0,f=1;char ch=getchar(); 14 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 15 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 16 return x*f; 17 } 18 struct edge{ 19 int t,v; 20 edge *next,*rev; 21 }e[nm],*h[NM],*o=e,*p[NM],*tmp[NM]; 22 int ans; 23 void _add(int x,int y,int v){ 24 o->v=v;o->t=y;o->next=h[x];h[x]=o;o++; 25 } 26 void add(int x,int y,int v){ 27 _add(x,y,v);_add(y,x,0); 28 h[x]->rev=h[y];h[y]->rev=h[x]; 29 if(v<inf)ans+=v; 30 } 31 int n,m,v,tot,b[105][105],d[NM],cnt[NM]; 32 int maxflow(){ 33 int flow=0;edge *j; 34 inc(i,1,n)tmp[i]=h[i]; 35 cnt[0]=v; 36 for(int x=0,s=inf;d[x]<v;){ 37 for(j=tmp[x];j;j=j->next) 38 if(j->v&&d[j->t]+1==d[x])break; 39 if(j){ 40 s=min(s,j->v);tmp[x]=p[j->t]=j; 41 if((x=j->t)==n){ 42 for(;x;x=p[x]->rev->t){ 43 p[x]->v-=s; 44 p[x]->rev->v+=s; 45 } 46 flow+=s;s=inf; 47 } 48 }else{ 49 if(!--cnt[d[x]])break; 50 d[x]=v; 51 link(x) 52 if(j->v&&d[j->t]+1<d[x]){ 53 d[x]=d[j->t]+1; 54 tmp[x]=j; 55 } 56 cnt[d[x]]++; 57 if(x)x=p[x]->rev->t; 58 } 59 } 60 return flow; 61 } 62 int main(){ 63 // freopen("data.in","r",stdin); 64 n=read();m=read(); 65 inc(i,1,n) 66 inc(j,1,m)b[i][j]=++tot; 67 v=++tot; 68 inc(i,1,n) 69 inc(j,1,m)add(0,b[i][j],read()); 70 inc(i,1,n) 71 inc(j,1,m)add(b[i][j],tot,read()); 72 inc(i,1,n-1) 73 inc(j,1,m){ 74 v++; 75 add(0,v,read()); 76 add(v,b[i][j],inf);add(v,b[i+1][j],inf); 77 } 78 inc(i,1,n-1) 79 inc(j,1,m){ 80 v++; 81 add(v,tot,read()); 82 add(b[i][j],v,inf);add(b[i+1][j],v,inf); 83 } 84 inc(i,1,n) 85 inc(j,1,m-1){ 86 v++; 87 add(0,v,read()); 88 add(v,b[i][j],inf);add(v,b[i][j+1],inf); 89 } 90 inc(i,1,n) 91 inc(j,1,m-1){ 92 v++; 93 add(v,tot,read()); 94 add(b[i][j],v,inf);add(b[i][j+1],v,inf); 95 } 96 n=tot;v++; 97 printf("%d\n",ans-maxflow()); 98 }