http://blog.csdn.net/u011742541/article/details/16989645
int SAP_Max_Flow( int st,int end )
{
int cur[maxn],pre[maxn],h[maxn],gap[maxn];
int u,neck,tmp,i;
memset( h,0,sizeof(h) );
memset( gap,0,sizeof(gap) );
int cur_flow,flow_ans = 0;
for( int i = 0; i <= n; i ++ )
cur[i] = head[i];
gap[0] = n+1; //顶点个数
u = st;
while( h[st] < n+1 ) //当起点的距离标号大于顶点个数时算法结束
{
if( u == end )
{
cur_flow = inf;
for( i = st; i != end; i = edge[cur[i]].v )
{
if( cur_flow > edge[cur[i]].w )
{
neck = i;
cur_flow = edge[cur[i]].w;
}
}
for( i = st; i != end; i = edge[cur[i]].v )
{
tmp = cur[i];
edge[tmp].w -= cur_flow;
edge[tmp^1].w += cur_flow;
}
flow_ans += cur_flow;
u = neck;
}
for( i = cur[u]; i != -1; i = edge[i].next )
{
if( edge[i].w && h[u] == h[edge[i].v] + 1 )
break;
}
if( i != -1 )
{
cur[u] = i;
pre[edge[i].v] = u;
u = edge[i].v;
}
else
{
if( 0 == --gap[h[u]] )
break;
cur[u] = head[u];
for( tmp = n,i = head[u]; i != -1; i = edge[i].next )
if( edge[i].w )
tmp = tmp < h[edge[i].v]?tmp:h[edge[i].v];
h[u] = tmp + 1;
gap[h[u]] ++;
if( u != st )
u = pre[u];
}
}
return flow_ans;
}