BZOJ P2132 圈地计划

同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 3
1 2 3
4 5 6
7 8 9
9 8 7
6 5 4
3 2 1
1 1 1
1 3 1
1 1 1

out:
81

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值