20分A 1 A 2 萌新求调
  • 板块P4474 王者之剑
  • 楼主Leo1108
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/12/12 17:17
  • 上次更新2024/12/12 20:26:56
查看原帖
20分A 1 A 2 萌新求调
759157
Leo1108楼主2024/12/12 17:17
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+5,maxm=6e4+5;
int n,m,s,t;
int ans;
int dep[maxn],cur[maxn];
int dd[5][2]={{0,0},{0,1},{0,-1},{1,0},{-1,0}};
struct aaa{
	long long x,val,t;
}e[maxm<<1];
int cnt,head[maxn];
void add(int x,int y,int w){
	e[cnt]={y,w,head[x]};
	head[x]=cnt;
	++cnt;
} 
bool bfs(){
	queue<int>q;
	memset(dep,0,sizeof(dep));
	q.push(s);
	dep[s]=1;
	cur[s]=head[s];
	while(q.size()){
		int u=q.front();
		q.pop();
		for(int i=head[u];~i;i=e[i].t){
			int to=e[i].x,w=e[i].val;
			if((!dep[to])&&w){
				cur[to]=head[to];
				dep[to]=dep[u]+1; 
				if(to==t){
					return 1;
				}
				q.push(to);
			}
		}
	}
	return 0;
}
int dfs(int x,int maxi){
	if(x==t){
		return maxi;
	}
	int rest=maxi;
	for(int i=cur[x];~i&&rest;i=e[i].t){
		cur[x]=i;
		int to=e[i].x,w=e[i].val;
		if(dep[to]==dep[x]+1&&w){
			int k=dfs(to,min(rest,w));
			if(!k){
				dep[to]=0;
			}
			rest-=k;
			e[i].val-=k;
			e[i^1].val+=k;
		} 
	}
	return maxi-rest;
}
int f(int x,int y){
	return (x-1)*m+y; 
}
int main(){
	cin>>n>>m;
	memset(head,-1,sizeof(head));
	int x;
	t=n*m+1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>x;
			ans+=x;
			for(int k=1;k<=4;k++){
				int xx=i+dd[k][0],yy=j+dd[k][1];
				if(xx>=1&&yy>=1&&xx<=n&&yy<=m){
					add(f(i,j),f(xx,yy),1e8+5);
					add(f(xx,yy),f(i,j),0); 
				}
			}
			if((i+j)&1){
				add(f(i,j),s,0);
				add(s,f(i,j),x);
			}
			else{
				add(f(i,j),t,x);
				add(t,f(i,j),0);
			} 
		}
	}
	int flow=0;
	while(bfs()){
		while(flow=dfs(s,2e9)){
			ans-=flow;
		}
	}
	cout<<ans;
	return 0;
} 
2024/12/12 17:17
加载中...