题目链接:
https://www.luogu.com.cn/problem/P2774
参考博客:
https://www.luogu.com.cn/blog/cicos/solution-p2774#
算法:1:最大流 最小割 Dinic
思路:
1:想象一下,每一个格子只有两种状态,被拿走,和留下,首先我们取所有的格子,然后呢,把其中的一些格子拿去,使留下的格子之间没有互斥的,什么是互斥,就是不可以同时留下的点
2:我们可以发现,所有互斥(题目所说的相邻)奇偶性一定不同,但是奇偶性不同的两个格子不一定互斥,但是可以肯定互斥的节点奇偶性一定不同
3:可以把整张图这样连,把节点按序号分为奇数一类,偶数一类,一类连源点,一类连汇点,权值为格子的值,均为单向边,奇数号格子和偶数号格子之间,如果互斥就相连,一个格子最多有4个互斥的格子,权值为inf,且也均为单向边(整张图均为单向边,存边的时候存的反向边权值均为0)
4:然后就可以割边,割到源点s到汇点t没有流为止,因为,只要还有流可以流到汇点t,就意味着在残图中一定还存在互斥的边,因为,连接奇数节点和偶数节点之间的都是互斥的边,s到t的流为0才说明所有的互斥边都不存在了,即肯定是至少这条边的两个端点之一已经被切去了,剩下的与源点s或汇点t相连的边的总和就是可以取的最大值,而切去的边就是最大流
一:代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e4+2,maxm=6e4+1,inf=1e9;
int m,n,a,s,t,tot=1,head[maxn],dep[maxn],ans,sum;
const int d[4][2]=
{
{0,1},{0,-1},{-1,0},{1,0}
};
struct edge
{
int to,next,w;
}e[maxm];
void addedge(int x,int y,int w)
{
e[++tot].to=y;
e[tot].w=w;
e[tot].next=head[x];
head[x]=tot;
}
bool bfs()
{
memset(dep,0,sizeof(dep));
queue<int>q;
q.push(s);dep[s]=1;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].next)
{
int y=e[i].to,w=e[i].w;
if(w&&!dep[y])
{
dep[y]=dep[x]+1;
q.push(y);
}
}
}
return dep[t];
}
int dfs(int x,int flow)
{
if(x==t)return flow;
int sum=0;
for(int i=head[x];i;i=e[i].next)
{
int y=e[i].to,w=e[i].w;
if(w&&dep[y]==dep[x]+1)
{
int t=dfs(y,min(flow,w));
e[i].w-=t;e[i^1].w+=t;
flow-=t;sum+=t;
}
}
if(!sum)dep[x]=0;
return sum;
}
int main()
{
ios::sync_with_stdio(0);
scanf("%d %d",&m,&n);
s=0,t=m*n+1;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a);
sum+=a;
if((i+j)%2==0)
{
addedge(s,(i-1)*n+j,a),addedge((i-1)*n+j,s,0);
for(int k=0;k<=3;k++)
{
int x=i+d[k][0],y=j+d[k][1];
if(1<=x&&x<=m&&1<=y&&y<=n)
{
addedge((i-1)*n+j,(x-1)*n+y,inf);
addedge((x-1)*n+y,(i-1)*n+j,0);
}
}
}
else
{
addedge((i-1)*n+j,t,a),addedge(t,(i-1)*n+j,0);
}
}
}
while(bfs())ans+=dfs(s,inf);
printf("%d\n",sum-ans);
return 0;
}
二:代码(写法二,算法和思路完全一样,用了define)
#include <bits/stdc++.h>
#define s 0
#define t (m*n+1)
#define xuhao(i,j) ((i-1)*n+j)
#define hefa(x,di,top) (di<=x&&x<=top)
using namespace std;
const int maxn=2e4+2,maxm=6e4+1,inf=1e9;
int m,n,a,tot=1,head[maxn],dep[maxn],ans,sum;
const int d[4][2]=
{
{0,1},{0,-1},{-1,0},{1,0}
};
struct edge
{
int to,next,w;
}e[maxm];
void addedge(int x,int y,int w)
{
e[++tot].to=y;
e[tot].w=w;
e[tot].next=head[x];
head[x]=tot;
}
bool bfs()
{
memset(dep,0,sizeof(dep));
queue<int>q;
q.push(s);dep[s]=1;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].next)
{
int y=e[i].to,w=e[i].w;
if(w&&!dep[y])
{
dep[y]=dep[x]+1;
q.push(y);
}
}
}
return dep[t];
}
int dfs(int x,int flow)
{
if(x==t)return flow;
int sum=0;
for(int i=head[x];i;i=e[i].next)
{
int y=e[i].to,w=e[i].w;
if(w&&dep[y]==dep[x]+1)
{
int f=dfs(y,min(flow,w));
e[i].w-=f;e[i^1].w+=f;
flow-=f;sum+=f;
}
}
if(!sum)dep[x]=0;
return sum;
}
int main()
{
ios::sync_with_stdio(0);
scanf("%d %d",&m,&n);
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a);
sum+=a;
if((i+j)%2==0)
{
addedge(s,xuhao(i,j),a),addedge(xuhao(i,j),s,0);
for(int k=0;k<=3;k++)
{
int x=i+d[k][0],y=j+d[k][1];
if(hefa(x,1,m)&&hefa(y,1,n))
{
addedge(xuhao(i,j),xuhao(x,y),inf);
addedge(xuhao(x,y),xuhao(i,j),0);
}
}
}
else
{
addedge(xuhao(i,j),t,a),addedge(t,xuhao(i,j),0);
}
}
}
while(bfs())ans+=dfs(s,inf);
printf("%d\n",sum-ans);
return 0;
}