WA36分求助
查看原帖
WA36分求助
341373
Autofreeze楼主2021/1/7 19:20

RT

#include<bits/stdc++.h>
#define N 2001001
#define re register
#define MAX 2001
#define num(i,j) (i-1)*m+j
#define inf 1e18
using namespace std;
typedef long long ll;
typedef double db;
inline void read(re ll &ret)
{
	ret=0;re char c=getchar();re bool pd=false;
	while(!isdigit(c)){pd|=c=='-';c=getchar();}
	while(isdigit(c)){ret=(ret<<1)+(ret<<3)+(c&15);c=getchar();}
	ret=pd?-ret:ret;
	return;
}
ll n,m,a[MAX][MAX],s,t,all,tot=-1,head[N],dep[N],now[N];
queue<ll>q;
struct edge
{
	ll from,to,dis,nxt;
}e[N];
inline void add(re ll u,re ll v,re ll d)
{
	e[++tot].from=u,e[tot].to=v,e[tot].dis=d,e[tot].nxt=head[u],head[u]=tot;
	e[++tot].from=v,e[tot].to=u,e[tot].dis=d,e[tot].nxt=head[v],head[v]=tot;
	return;
}
inline bool bfs(re ll s,re ll t)
{
	while(!q.empty())
		q.pop();
	memset(dep,0,(t+1)*sizeof(ll));
	dep[s]=1;
	q.push(s);
	now[s]=head[s];
	while(!q.empty())
	{
		re ll ver=q.front();
		q.pop();
		for(re int i=head[ver];i^(-1);i=e[i].nxt)
		{
			if(e[i].dis>0&&!dep[e[i].to])
			{
				dep[e[i].to]=dep[ver]+1;
				now[e[i].to]=head[e[i].to];
				q.push(e[i].to);
			}
		}
	}
	return dep[t];
}
inline ll dfs(re ll ver,re ll lim)
{
	if(ver==t)
		return lim;
	re ll out=0;
	for(re int i=now[ver];i^(-1);i=e[i].nxt)
	{
		now[ver]=i;
		if(e[i].dis&&dep[e[i].to]==dep[ver]+1)
		{
			re ll tmp=dfs(e[i].to,min(lim,e[i].dis));
			e[i].dis-=tmp;
			e[i^1].dis+=tmp;
			lim-=tmp;
			out+=tmp;
		}
	}
	if(!out)
		dep[ver]=0;
	return out;
}
inline ll dinic(re ll s,re ll t)
{
	re ll ret=0;
	while(bfs(s,t))
		ret+=dfs(s,inf);
	return ret;
}
signed main()
{
	memset(head,-1,sizeof(head));
	read(n);
	read(m);
	s=0,t=n*m+1;
	for(re int i=1;i<=n;i++)
		for(re int j=1;j<=m;j++)
		{
			read(a[i][j]);
			all+=a[i][j];
			if((i+j)&1)
				add(s,num(i,j),a[i][j]),add(num(i,j),s,0);
			else
				add(num(i,j),t,a[i][j]),add(t,num(i,j),0);
		}
	for(re int i=1;i<=n;i++)
		for(re int j=1;j<=m;j++)
		{
			if((i+j)&1)
			{
				if(i>1)
					add(num(i,j),num(i-1,j),inf),add(num(i-1,j),num(i,j),0);
				if(i<n)
					add(num(i,j),num(i+1,j),inf),add(num(i+1,j),num(i,j),0);
				if(j>1)
					add(num(i,j),num(i,j-1),inf),add(num(i,j-1),num(i,j),0);
				if(j<m)
					add(num(i,j),num(i,j+1),inf),add(num(i,j+1),num(i,j),0);
			}
		}
	printf("%lld\n",all-dinic(s,t));
	exit(0);
}
2021/1/7 19:20
加载中...