BZOJ P2132 圈地计划

来源:互联网 发布:乔任梁死亡真相知乎 编辑:程序博客网 时间:2024/04/27 16:46

同happiness

dfs写错调了半天的很弱的我

#include<iostream>#include<fstream>#include<algorithm>#include<cmath>#include<cstring>using namespace std;const int inf=0x7fffffff;const int T=10001;int n,m,cnt=1,tot,ans;int head[10003],h[10003];int a[103][103],b[103][103],c[103][103];bool color[103][103];int xx[4]={0,0,1,-1},yy[4]={1,-1,0,0};int q[10003];struct data{int to,next,v;}e[100003];void ins(int u,int v,int w){e[++cnt].to=v;e[cnt].v=w;e[cnt].next=head[u];head[u]=cnt;}void insert(int u,int v,int w){tot+=w;ins(u,v,w);ins(v,u,0);}bool bfs(){memset(h,-1,sizeof(h));int t=0,w=1;q[0]=h[0]=0;while(t!=w){int now=q[t++];t%=10001;for(int i=head[now];i;i=e[i].next){if(e[i].v&&h[e[i].to]<0){h[e[i].to]=h[now]+1;q[w++]=e[i].to;w%=10001;}}}if(h[T]==-1){return false;}else{return true;}}int dfs(int now,int f){if(now==T){return f;}int used=0;for(int i=head[now];i;i=e[i].next){if(h[e[i].to]==h[now]+1&&e[i].v){int w=f-used;w=dfs(e[i].to,min(e[i].v,w));e[i].v-=w; e[i^1].v+=w;  used+=w;if(used==f){return f;}}}if(used==0){h[now]=-1;}return used;}void dinic(){while(bfs()){ans+=dfs(0,inf);}}void build(){for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(color[i][j]){swap(a[i][j],b[i][j]);}insert(0,(i-1)*m+j,a[i][j]);insert((i-1)*m+j,T,b[i][j]);if(color[i][j]){for(int k=0;k<=3;k++){int nowx=i+xx[k],nowy=j+yy[k];if(nowx>n||nowy>m||nowx<1||nowy<1){continue;}ins((i-1)*m+j,(nowx-1)*m+nowy,c[i][j]+c[nowx][nowy]);ins((nowx-1)*m+nowy,(i-1)*m+j,c[i][j]+c[nowx][nowy]);tot+=(c[i][j]+c[nowx][nowy]);}}}}}int main(){cin>>n>>m;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cin>>a[i][j];}}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cin>>b[i][j];}}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cin>>c[i][j];}}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if((i+j)&1){color[i][j]=1;}}}build();   dinic();cout<<tot-ans<<endl;return 0;}/*in:3 31 2 34 5 67 8 99 8 76 5 43 2 11 1 11 3 11 1 1out:81*/


1 0