题解:
先奖每一个点裂开来
然后在见图
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=;
int fi[N],n,t,a[][],cas,m,x,y,z,f[N],ne[N],num,zz[N],fl[N],gp[N],dist[N],pre[N],sl[N];
void jb(int x,int y,int z,int s)
{
ne[num]=fi[x];
fi[x]=num;
zz[num]=y;
sl[num]=z;
fl[num++]=s;
ne[num]=fi[y];
fi[y]=num;
zz[num]=x;
sl[num]=;
fl[num++]=-s;
}
int spfa()
{
memset(dist,0x3f,sizeof dist);
memset(pre,-,sizeof pre);
memset(gp,,sizeof gp);
memset(f,,sizeof f);
queue<int > Q;
Q.push();
dist[]=;
while (!Q.empty())
{
int now=Q.front();
Q.pop();
f[now]=;
for (int i=fi[now];i!=-;i=ne[i])
if (sl[i]>)
{
int t=zz[i];
if (dist[t]>dist[now]+fl[i])
{
dist[t]=dist[now]+fl[i];
pre[t]=now;
gp[t]=i;
if (!f[t])
{
f[t]=;
Q.push(t);
}
}
}
}
if (pre[n+]==-)return ;
return ;
}
void Max_flow()
{
int cost=,flow=;
while (!spfa())
{
int f=1e9;
for (int i=n+;i>;i=pre[i])
f=min(f,sl[gp[i]]);
cost+=f;
flow+=dist[n+]*f;
for (int i=n+;i>;i=pre[i])
{
sl[gp[i]]-=f;
sl[gp[i]^]+=f;
}
}
printf("%d\n",-flow);
}
int main()
{
memset(fi,-,sizeof fi);
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
scanf("%d",&a[i][j]);
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
{
int x=(i-)*n+j+;
int y=x+n*n;
jb(x,y,,-a[i][j]);
jb(x,y,m,);
if (i<n)jb(y,x+n,m,);
if (j<n)jb(y,x+,m,);
}
jb(,,m,);
jb(*n*n+,*n*n+,m,);
n=*n*n;
Max_flow();
}