The maximum flow notes

Read one afternoon, half-comprehended just do not understand ah. .
First code live! Figure also build giant said than done! !
Bfs is probably thinking:
Every bfs all over again, if you can also find answers to augmenting path is recorded
each time to sort out the answer together, at the same time a wonderful operation: precursor plus minus successor.
I understand and feel the same mark dfs finished cancellation feeling that this step in order to eliminate the impact on future behavior caused, easy to find the optimal solution. `

1. Storage

struct p{
 int s,e,num;
} flow[maxn];
//没毛病
for(int i=1;i<=m;i++){
  int u,v,w;
  scanf("%d%d%d",&u,&v,&w);
  flow[i].s=u;
  flow[i].e=v;
  flow[i].num=w;
 }

2. Key However, I will not ah


//大概思路 EK(start,end) 如果还能找到就更新
int EK(int s_pos,int e_pos){
 int minn;
 while(bfs(s_pos,e_pos)){
  minn=inf;
  //结构体就是遍历的时候麻烦· 是倒着遍历的
  for(int i=e_pos;i!=s;i=flow[pre[i]].s){
   minn=min(minn,flow[pre[i]].num);
   //flow是从pre[i]出发找到的增广路
  }
  for(int i=e_pos;i!=s;i=flow[pre[i]].s){
   flow[pre[i]].num-=minn;
   //这里我不是很明白。。。
   flow[pre[i]+m].num+=minn;
  }
  max_flow+=minn;
 }
 return max_flow;
}

bfs concrete realization


bool  bfs(int s,int e){
 //初始化  只要设置成不一样的 
 s_pos=-1,e_pos=0;
 memset(wh,0,sizeof wh);
 //标记已经访问
 wh[s]=1;
 q[0]=s;//q是个队列来着
 while(s_pos!=e_pos){
  cur_pos=q[++s_pos];/*
  for(int i=1;i<=n;++i){ 
   if(!wh[i] && flow[cur_pos][i]>0){
    wh[i]=1;//标记往下走 
    pre[i]=cur_pos;
    if(i==e) return 1;
    //?
    q[++e_pos]=i;
   }
  } */
  for(int i=1;i<=2*m;i++){
   if(cur_pos == flow[i].s &&
   !wh[flow[i].e] && flow[i].num){
    wh[flow[i].e]=1;
    pre[flow[i].e]=i;
    if(flow[i].e == e) return 1;
    q[++e_pos]=flow[i].e;
   }
  }
 }
 return 0;
}

This is still time out, wait for me to take another look.

Published 24 original articles · won praise 2 · Views 972

Guess you like

Origin blog.csdn.net/weixin_43521836/article/details/88914556