Edmonds-Karp算法

建立在Ford-Fulkerson 方法上的增广路算法,与一般的Ford-Fulkerson 算法不同的是,它用广度搜索实现对增广路的寻找

  
    
/* ********************************************************************** */
/* Name: maxflow
/* Description: find the max flow of the network from s to t using
Edmonds-Karp Algorithm
/* Parameter list: s - begin point of the network
/* t - end point of the network
/***********************************************************************
*/
#define MAX_VECT 105
#define INF 105
int c[MAX_VECT][MAX_VECT];
int n;
int maxflow( int s, int t)
{
int u, v, bg, ed, minflow, flow = 0 ;
int queue[MAX_VECT], pre[MAX_VECT];
while ( true )
{
memset(pre,
- 1 , sizeof (pre));
for (queue[bg = ed = 0 ] = s; bg <= ed; bg ++ )
{
u
= queue[bg];
for ( int i = 0 ; (i < n) && (pre[t] ==- 1 ); i ++ )
if (c[u][i] > 0 && pre[i] == - 1 )
{
pre[i]
= u;
queue[
++ ed] = i;
}
}
if (pre[t] == - 1 ) break ;
minflow
= INF;
for (u = pre[v = t];v != s;v = u,u = pre[v])
if (c[u][v] < minflow) minflow = c[u][v];
for (u = pre[v = t];v != s;v = u,u = pre[v])
c[u][v]
-= minflow, c[v][u] += minflow;
flow
+= minflow;
}
return flow;
}

 

你可能感兴趣的:(算法)