题目连接:
https://www.luogu.com.cn/problem/P2045
思路来源博客:https://www.luogu.com.cn/blog/xht37/p2045-fang-ge-qu-shuo-jia-qiang-ban
和我的代码类似博客:https://www.luogu.com.cn/blog/user48036/solution-p2045
算法:
1:最大费用(花费边权取负值) 最大流 Dinic+Spfa
2:特别注意:边权取负,求最短路,就是最长路,也就是最大费用
思路:
1: 点边转化:把每个格子 (i,j) 拆成一个入点一个出点(入点,是对一个节点而言,只有从它的入点进来,然后从它的出点出去,才算走过了这个点)
2:从每个入点向对应的出点连两条有向边:一条容量为 1 ,费用为格子 (i,j) 中的数的负值;另一条容量为 k−1 ,费用为 0
3:从 (i,j) 的出点到 (i,j+1) 和 (i+1,j) 的入点连有向边,容量为 k ,费用为 0
4:以 (1,1)的入点为源点, (n,n) 的出点为汇点,求最大费用最大流
代码一:
#include <bits/stdc++.h>
#define xuhao(i,j) ((i-1)*n+j)
#define hefa(x,di,top) (di<=x&&x<=top)
using namespace std;
const int maxn=5e3+2,maxm=2e4+1,inf=1<<31-1;
int n,k,s,t,a,tot=1,head[maxn],dis[maxn],flow[maxn],pre[maxn],last[maxn],maxflow,mincost;
bool vis[maxn];
queue<int>q;
struct edge
{
int to,next,w,dis;
}e[maxm];
void addedge(int x,int y,int w,int dis)
{
e[++tot].to=y;
e[tot].w=w;
e[tot].dis=dis;
e[tot].next=head[x];
head[x]=tot;
}
bool spfa(int s,int t)
{
memset(dis,0x7f,sizeof(dis));
memset(flow,0x7f,sizeof(flow));
memset(vis,0,sizeof(vis));
q.push(s);vis[s]=1;dis[s]=0;pre[t]=-1;
while(!q.empty())
{
int now=q.front();q.pop();
vis[now]=0;
for(int i=head[now];i!=-1;i=e[i].next)
{
int y=e[i].to;
if(e[i].w>0&&dis[y]>dis[now]+e[i].dis)
{
dis[y]=dis[now]+e[i].dis;
pre[y]=now;
last[y]=i;
flow[y]=min(flow[now],e[i].w);
if(!vis[y])
{
q.push(y);
vis[y]=1;
}
}
}
}
return pre[t]!=-1;
}
void dfs()
{
int now=t;
maxflow+=flow[t];
mincost+=flow[t]*dis[t];
while(now!=s)
{
e[last[now]].w-=flow[t];
e[last[now]^1].w+=flow[t];
now=pre[now];
}
}
int main()
{
ios::sync_with_stdio(0);
scanf("%d %d",&n,&k);
s=1,t=n*n*2;
memset(head,-1,sizeof(head));
// addedge(s1,xuhao(1,1),k,0),addedge(xuhao(1,1),s1,0,0);
// addedge(xuhao(n,n)+n*n,t1,k,0),addedge(t1,xuhao(n,n)+n*n,0,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a);a=-a;
addedge(xuhao(i,j),xuhao(i,j)+n*n,1,a),addedge(xuhao(i,j)+n*n,xuhao(i,j),0,-a);
addedge(xuhao(i,j),xuhao(i,j)+n*n,k-1,0),addedge(xuhao(i,j)+n*n,xuhao(i,j),0,0);
if(i<n)addedge(xuhao(i,j)+n*n,xuhao(i+1,j),k,0),addedge(xuhao(i+1,j),xuhao(i,j)+n*n,0,0);
if(j<n)addedge(xuhao(i,j)+n*n,xuhao(i,j+1),k,0),addedge(xuhao(i,j+1),xuhao(i,j)+n*n,0,0);
}
}
while(spfa(s,t))dfs();
printf("%d\n",-mincost);
return 0;
}
代码二:
#include <bits/stdc++.h>
#define s1 0
#define t1 (n*n*2+1)
#define xuhao(i,j) ((i-1)*n+j)
#define hefa(x,di,top) (di<=x&&x<=top)
using namespace std;
const int maxn=5e3+2,maxm=2e4+1,inf=1<<31-1;
int n,k,a,tot=1,head[maxn],dis[maxn],flow[maxn],pre[maxn],last[maxn],maxflow,mincost;
bool vis[maxn];
queue<int>q;
struct edge
{
int to,next,w,dis;
}e[maxm];
void addedge(int x,int y,int w,int dis)
{
e[++tot].to=y;
e[tot].w=w;
e[tot].dis=dis;
e[tot].next=head[x];
head[x]=tot;
}
bool spfa(int s,int t)
{
memset(dis,0x7f,sizeof(dis));
memset(flow,0x7f,sizeof(flow));
memset(vis,0,sizeof(vis));
q.push(s);vis[s]=1;dis[s]=0;pre[t]=-1;
while(!q.empty())
{
int now=q.front();q.pop();
vis[now]=0;
for(int i=head[now];i!=-1;i=e[i].next)
{
int y=e[i].to;
if(e[i].w>0&&dis[y]>dis[now]+e[i].dis)
{
dis[y]=dis[now]+e[i].dis;
pre[y]=now;
last[y]=i;
flow[y]=min(flow[now],e[i].w);
if(!vis[y])
{
q.push(y);
vis[y]=1;
}
}
}
}
return pre[t]!=-1;
}
void dfs()
{
int now=t1;
maxflow+=flow[t1];
mincost+=flow[t1]*dis[t1];
while(now!=s1)
{
e[last[now]].w-=flow[t1];
e[last[now]^1].w+=flow[t1];
now=pre[now];
}
}
int main()
{
ios::sync_with_stdio(0);
scanf("%d %d",&n,&k);
memset(head,-1,sizeof(head));
addedge(s1,xuhao(1,1),k,0),addedge(xuhao(1,1),s1,0,0);
addedge(xuhao(n,n)+n*n,t1,k,0),addedge(t1,xuhao(n,n)+n*n,0,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a);a=-a;
addedge(xuhao(i,j),xuhao(i,j)+n*n,1,a),addedge(xuhao(i,j)+n*n,xuhao(i,j),0,-a);
addedge(xuhao(i,j),xuhao(i,j)+n*n,k-1,0),addedge(xuhao(i,j)+n*n,xuhao(i,j),0,0);
if(i<n)addedge(xuhao(i,j)+n*n,xuhao(i+1,j),k,0),addedge(xuhao(i+1,j),xuhao(i,j)+n*n,0,0);
if(j<n)addedge(xuhao(i,j)+n*n,xuhao(i,j+1),k,0),addedge(xuhao(i,j+1),xuhao(i,j)+n*n,0,0);
}
}
while(spfa(s1,t1))dfs();
printf("%d\n",-mincost);
return 0;
}