Using the code solve max_flow problem.I just using the mould.I don't know the details of isap algorithm.
ps:This is my first solve Network Flow problem.
The portal : http://acm.hdu.edu.cn/showproblem.php?pid=1532
#include<iostream>
#include <cstdio>
#include <cmath>
#include <memory.h>
#define MAXN 100005
#define MAXE 2000000
#define INF 0x3f3f3f3f
using namespace std;
int tmp,src,des,cnt;
int n,m;
struct Edge{
int from,to;
int next,cap;
}edge[MAXE];
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN],stack[MAXN],top;
int isap(){
int cur_flow,max_flow,u,insert,i;
memset(dep,0,sizeof(dep));
memset(gap,0,sizeof(gap));
memcpy(cur,head,n);
max_flow = 0;
u = src;
top = 0;
while(dep[src] < n){
if(u == des){
cur_flow = INF;
for(i = 0;i < top;i++){
if(cur_flow > edge[stack[i]].cap){
insert = i;
cur_flow = edge[stack[i]].cap;
}
}
for(i = 0;i < top;i++){
edge[stack[i]].cap -= cur_flow;
edge[stack[i]^1].cap += cur_flow;
}
max_flow += cur_flow;
u = edge[stack[top = insert]].from;
}
for(i = cur[u];i != -1;i=edge[i].next){
if((edge[i].cap > 0)&&(dep[u] == dep[edge[i].to]+1))
break;
}
if(i != -1){
stack[top++] = i;
u = edge[i].to;
}
else{
if(0 == --gap[dep[u]])break;
int minn = n;
for(i = head[u];i!=-1;i=edge[i].next){
if(edge[i].cap > 0)
minn = (minn > dep[edge[i].to]) ? (cur[u] = i,dep[edge[i].to]) : minn;
}
++gap[dep[u] = minn + 1];
if(u != src) u = edge[stack[--top]].from;
}
}
return max_flow;
}
void addedge(int u,int v,int f){
edge[cnt].next = head[u];
edge[cnt].from = u;
edge[cnt].to = v;
edge[cnt].cap = f;
head[u] = cnt ++;
edge[cnt].next = head[v];
edge[cnt].from = v;
edge[cnt].to = u;
edge[cnt].cap = 0;
head[v] = cnt ++;
}
int main(void){
while(~scanf("%d%d",&m,&n)){
cnt = 0;
src = 0;
des = n-1;
memset(head,-1,sizeof(head));
while(m--){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
--a; --b;
addedge(a,b,c);
}
int ans = isap();
printf("%d\n",ans);
}
return 0;
}