zkw费用流 java_zkw费用流算法

zkw算法就是在求最短路的基础上进行多路增广的一种算法,是SPFA求最小费用增广路的一种优化算法。其中用距离标号来求最短路,距离标号类似于预流推进算法中的势函数。

算法流程:

将全部距离标号清零,最初子图只有源点。

利用DFS在子图上求最小费用增广路,如果无法求,则转3。

调整距离标号,使得在最小增量的情况下有一个点进入子图,转2,如果无法调整,结束。

代码(从1到n的费用流):

#include

#include

#include

using namespace std;

#define MAXN 2001

int dist[MAXN];

bool f[MAXN];

struct node{

int t,f,c;

node *next,*pair;

} *head[MAXN];

int n,m;

int mincost=0;

void INSERT(int s,int t,int f,int c){

node *p=new(node);

p->t=t;

p->f=f;

p->c=c;

p->next=head[s];

head[s]=p;

}

int aug(int v,int flow){

if (v==n){

mincost+=dist[1]*flow;

return flow;

}

f[v]=false;

int rec=0;

for (node *p=head[v];p;p=p->next){

if (f[p->t]&&dist[v]==dist[p->t]+p->c&&p->f){

int ret=aug(p->t,min(flow-rec,p->f));

p->f-=ret;

p->pair->f+=ret;

if ((rec+=ret)==flow){

return flow;

}

}

}

return rec;

}

bool relabel(){

int rec=0x7fffffff;

for (int i=0;i++

if (f[i]){

continue;

}

for (node *p=head[i];p;p=p->next){

if (f[p->t]&&p->f){

rec=min(rec,dist[p->t]-dist[i]+p->c);

}

}

}

if (rec==0x7fffffff){

return false;

}

for (int i=0;i++

if (!f[i]){

dist[i]+=rec;

}

}

return true;

}

int main(){

scanf("%d%d",&n,&m);

for (int i=0;i++

head[i]=NULL;

}

while (m--){

int s,t,f,c;

scanf("%d%d%d%d",&s,&t,&f,&c);

INSERT(s,t,f,c);

INSERT(t,s,0,-c);

head[s]->pair=head[t];

head[t]->pair=head[s];

}

memset(dist,0,sizeof(dist));

do {

do {

memset(f,true,sizeof(f));

} while (aug(1,0x7fffffff));

} while (relabel());

printf("%d\n",mincost);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值