网络流模板.docx

上传人:b****6 文档编号:6647904 上传时间:2023-01-08 格式:DOCX 页数:20 大小:19.06KB
下载 相关 举报
网络流模板.docx_第1页
第1页 / 共20页
网络流模板.docx_第2页
第2页 / 共20页
网络流模板.docx_第3页
第3页 / 共20页
网络流模板.docx_第4页
第4页 / 共20页
网络流模板.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

网络流模板.docx

《网络流模板.docx》由会员分享,可在线阅读,更多相关《网络流模板.docx(20页珍藏版)》请在冰豆网上搜索。

网络流模板.docx

网络流模板

<网络流>

最大流:

Ford&Fulkerson算法

#include

#include

#include

usingnamespacestd;

constintmaxint=100000000;

#defineMAX410

intn,m;

intcap[MAX][MAX],prev[MAX],s,t,flow[MAX][MAX];

intfind()

{

intQ[MAX],head=0,tail=0;;

memset(prev,-1,sizeof(prev));

Q[tail++]=s;

prev[s]=s;

while(head

{

intp=Q[head++];

for(inti=1;i<=n;i++)

{

if(prev[i]==-1&&cap[p][i]-flow[p][i]>0)

{

prev[i]=p;

if(i==t)

return1;

Q[tail++]=i;

}

}

}

return0;

}

intmaxflow()

{

intex,i,ret=0;

while(find())

{

ex=maxint;

for(i=t;i!

=s;i=prev[i])

{

if(ex>cap[prev[i]][i]-flow[prev[i]][i])

ex=cap[prev[i]][i]-flow[prev[i]][i];

}

for(i=t;i!

=s;i=prev[i])

{

flow[prev[i]][i]+=ex;

flow[i][prev[i]]-=ex;

}

ret+=ex;

}

returnret;

}

最大流:

Dinic算法

#include

#include

usingnamespacestd;

#defineMAXN20010

#defineinf0x7fffffff

structnode{

intnext,c,f,other;

}N;

vectormap[MAXN];//原图

vectorlever_map[MAXN];//层次图

intque[MAXN*1],lever[MAXN],pre[MAXN],hash[MAXN],d[MAXN];

ints,t;

voidinit(){

inti;

for(i=s;i<=t;i++){

map[i].clear();

}

}

voidadd(intu,intv,intc){

N.next=v;N.c=c;N.other=map[v].size();N.f=0;

map[u].push_back(N);

N.next=u;N.c=0;N.other=map[u].size()-1;N.f=0;

map[v].push_back(N);

}

boolbulid(){

inthead=0,tail=0,cur,next,i,j;

for(i=s;i<=t;i++)lever_map[i].clear();

memset(lever,-1,sizeof(lever));

que[tail++]=s;

lever[s]=0;

while(head

cur=que[head++];

for(i=0;i

N=map[cur][i];

if(N.c>N.f){

if(lever[N.next]==-1){

que[tail++]=N.next;

lever[N.next]=lever[cur]+1;

}

if(lever[N.next]==lever[cur]+1){

lever_map[cur].push_back(i);

}

}

}

}

returnlever[t]!

=-1;

}

intDinic(){

inti,j,ans=0,len,tmp;

while(bulid()){

memset(hash,0,sizeof(hash));

while(!

hash[s]){

d[s]=inf;

pre[s]=-1;

for(i=s;i!

=t&&i!

=-1;i=j){

len=lever_map[i].size();

while(len&&hash[map[i][lever_map[i][len-1]].next])

{lever_map[i].pop_back();len--;}

if(!

len){

hash[i]=1;

j=pre[i];

continue;

}

j=map[i][lever_map[i][len-1]].next;

pre[j]=i;

d[j]=min(d[i],map[i][lever_map[i][len-1]].c-map[i][lever_map[i][len-1]].f);

}

if(i==t){

ans+=d[t];

tmp=d[t];

while(i!

=s){

j=pre[i];

len=lever_map[j][lever_map[j].size()-1];

map[j][len].f+=tmp;

if(map[j][len].f==map[j][len].c)

lever_map[j].pop_back();

map[i][map[j][len].other].f-=tmp;

i=j;

}

}

}

}

returnans;

}

intmain(){

intn,m,i,a,b,w;

//freopen("Dinic.in","r",stdin);

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

s=0;t=n+1;

init();

for(i=1;i<=n;i++){

scanf("%d%d",&a,&b);

add(s,i,a);

add(i,t,b);

}

for(i=1;i<=m;i++){

scanf("%d%d%d",&a,&b,&w);

add(a,b,w);

add(b,a,w);

}

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

}

 

最大流:

ek算法

#include

#include

#include

usingnamespacestd;

//constintN=128;

//constintINF=1<<28;

template

classGraph{

private:

intv,s,t,prev[N];

Tcap[N][N],flow[N][N];

boolbfs();

public:

voidclear();

voidsetMaxNode(intn){v=n;}

voidinsert(intnu,intnv,intc){cap[nu][nv]+=c;}

TgetFlow(intnu,intnv){returnflow[nu][nv];}

TmaxFlow(int,int);

};

template

voidGraph:

:

clear(){

v=0;

for(inti=0;i

for(intj=0;j

cap[i][j]=T(0);

}

template

boolGraph:

:

bfs(){

queueQ;

boolvst[N]={false};

Q.push(s);vst[s]=true;

while(!

Q.empty()){

intu=Q.front();Q.pop();

for(inti=0;i

if(!

vst[i]&&cap[u][i]!

=flow[u][i]){

Q.push(i);prev[i]=u;vst[i]=true;

if(i==t)returntrue;

}

}

returnfalse;

}

template

TGraph:

:

maxFlow(intss,inttt){

s=ss;t=tt;

for(inti=0;i

for(intj=0;j

flow[i][j]=T(0);

Tf=0;

while(bfs()){

Tex=T(INF);

for(intc=t;c!

=s;c=prev[c])ex

=cap[prev[c]][c]-flow[prev[c]][c];

for(intc=t;c!

=s;c=prev[c])

{flow[prev[c]][c]+=ex;flow[c][prev[c]]-=ex;}

f+=ex;

}

returnf;

}

最大流:

dsp

#include

#include

#include

#include

usingnamespacestd;

//constintN=5120;

//constintINF=1<<28;

classEdge{

public:

intu,v,cuv,cvu,flow;

Edge(){}

Edge(intcu,intcv,intccu,intccv):

u(cu),v(cv),cuv(ccu),cvu(ccv),flow(0){}

intother(intp)const{returnp==u?

v:

u;}

intcap(intp)const{returnp==u?

cuv-flow:

cvu+flow;}

voidaddFlow(intp,intf){flow+=(p==u?

f:

-f);}

};

classGraph{

private:

vectoreg;

vectornet[N];

Edge*prev[N];

intv,s,t;

inth[N],hn[2*N],cur[N];

voidinitNet();

voidinitFlow();

voidinitHeight();

voidgapHeuristic(int);

public:

voidclear(){eg.clear();v=0;}

voidsetMaxNode(intcv){v=cv;}

voidinsert(intcu,intcv,intccu,intccv=0)

{eg.push_back(Edge(cu,cv,ccu,ccv));}

intmaxFlow(int,int);

};

voidGraph:

:

initHeight(){

memset(h,0,sizeof(h));memset(hn,0,sizeof(hn));

for(inti=0;i

queueQ;Q.push(t);h[t]=0;

while(!

Q.empty()){

intp=Q.front();Q.pop();

for(inti=net[p].size()-1;i>=0;i--){

intu=net[p][i]->other(p),ec=net[p][i]->cap(u);

if(ec!

=0&&h[u]==v){h[u]=h[p]+1;Q.push(u);}

}

}

for(inti=0;i

}

voidGraph:

:

gapHeuristic(intk){

if(hn[k]!

=0)return;

for(inti=0;i

if(h[i]>k)h[i]=v;

for(inti=k;i

{hn[v]+=hn[i];hn[i]=0;}

}

voidGraph:

:

initNet(){

for(inti=0;i

for(inti=eg.size()-1;i>=0;i--){

net[eg[i].u].push_back(&eg[i]);

net[eg[i].v].push_back(&eg[i]);

}

}

voidGraph:

:

initFlow(){

initNet();initHeight();

for(inti=0;i

}

intGraph:

:

maxFlow(intss,inttt){

s=ss;t=tt;initFlow();

intc=s,pre[N],flow=0;

pre[s]=-1;

while(h[s]

for(;cur[c]>=0;cur[c]--)

if(net[c][cur[c]]->cap(c)!

=0&&h[c]==h[net[c][cur[c]]->other(c)]+1)break;

if(cur[c]<0){

intmh=INF,oh=h[c];

for(inti=net[c].size()-1;i>=0;i--)

if(net[c][i]->cap(c)!

=0)mh

=h[net[c][i]->other(c)];

if(mh==INF)h[c]=v;

else{h[c]=mh+1;cur[c]=net[c].size()-1;}

hn[oh]--;hn[h[c]]++;gapHeuristic(oh);

if(c!

=s)c=pre[c];

}else{

intp=net[c][cur[c]]->other(c);

prev[p]=net[c][cur[c]];

pre[p]=c;c=p;

if(c==t){

intex=INF;

for(;c!

=s;c=pre[c])ex

=prev[c]->cap(pre[c]);

for(c=t;c!

=s;c=pre[c])prev[c]->addFlow(pre[c],ex);

flow+=ex;c=s;

}

}

}

returnflow;

}

最大流:

hlpp算法

#include

#include

#include

#include

usingnamespacestd;

//constintN=512;

//constintINF=1<<28;

classEdge{

public:

intu,v,cuv,cvu,flow;

Edge(){}

Edge(intcu,intcv,intccu,intccv):

u(cu),v(cv),cuv(ccu),cvu(ccv),flow(0){}

intother(intp)const{returnp==u?

v:

u;}

intcap(intp)const{returnp==u?

cuv-flow:

cvu+flow;}

voidaddFlow(intp,intf){flow+=(p==u?

f:

-f);}

};

classNodeList{

private:

intlevel,next[N],index[2*N],v;

public:

voidclear(intcv){v=cv;level=-1;memset(index,-1,sizeof(index));}

voidinsert(intn,inth){next[n]=index[h];index[h]=n;level>?

=h;}

intremove();

boolempty()const{returnlevel<0;}

};

intNodeList:

:

remove(){

intr=index[level];index[level]=next[index[level]];

while(level>=0&&index[level]==-1)level--;

returnr;

}

classGraph{

private:

vectoreg;

vectornet[N];

intn,v,s,t;

NodeListlist;

inth[N],hn[2*N],e[N],cur[N];

voidinitNet();

voidinitFlow();

voidinitHeight();

voidpush(int);

voidrelabel(int);

voiddischarge(int);

voidgapHeuristic(int);

public:

voidclear(){eg.clear();v=0;}

voidsetMaxNode(intcv){v=cv;}

voidinsert(intcu,intcv,intccu,intccv=0)

{eg.push_back(Edge(cu,cv,ccu,ccv));}

intmaxFlow(int,int);

};

voidGraph:

:

gapHeuristic(intk){

if(hn[k]!

=0||k>=v+1)return;

for(inti=0;i

if(h[i]>k&&h[i]<=v&&i!

=s)

{hn[h[i]]--;hn[v+1]++;h[i]=v+1;}

}

voidGraph:

:

initNet(){

for(inti=0;i

for(inti=eg.size()-1;i>=0;i--){

net[eg[i].u].push_back(&eg[i]);

net[eg[i].v].push_back(&eg[i]);

}

}

voidGraph:

:

initHeight(){

memset(h,0,sizeof(h));memset(hn,0,sizeof(hn));

memset(e,0,sizeof(e));

for(inti=0;i

queueQ;Q.push(t);h[t]=0;

while(!

Q.empty()){

intp=Q.front();Q.pop();

for(inti=net[p].size()-1;i>=0;i--){

intu=net[p][i]->other(p),ec=net[p][i]->cap(u);

if(ec!

=0&&h[u]==v&&u!

=s){h[u]=h[p]+1;Q.push(u);}

}

}

for(inti=0;i

}

voidGraph:

:

initFlow(){

initNet();initHeight();

for(inti=0;i

list.clear(v);

for(;cur[s]>=0;cur[s]--)push(s);

}

voidGraph:

:

push(intu){

Edge*te=net[u][cur[u]];

intex=te->cap(u),p=te->other(u);

if(ex==0)return;

if(u!

=s){ex

=e[u];e[u]-=ex;}

if(e[p]==0&&p!

=t&&p!

=s)list.insert(p,h[p]);

te->addFlow(u,ex);e[p]+=ex;

}

voidGraph:

:

relabel(intu){

intmh=2*v,oh=h[u];

for(inti=net[u].size()-1;i>=0;i--){

i

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1