hdu-4280

//最大流优化,sap模板

 

//代码如下:

 

#include
#include
#include

using namespace std;

#define N 100010
#define M 400010
#define INF 0x3f3f3f3f

int from[M],to[M],next[M],cap[M],head[N],ant;

void init(){ ant=0;memset(head,-1,sizeof(head));}
void add(int a,int b,int c){
    to[ant]=b;
    cap[ant]=c;
    from[ant]=a;
    next[ant]=head[a];
    head[a]=ant++;

    to[ant]=a;
    cap[ant]=0;
    from[ant]=b;
    next[ant]=head[b];
    head[b]=ant++;
}

int dp[N],num[N];
void bfs(int s,int e){
    int u,i;
    memset(num,0,sizeof(num));
    memset(dp,32,sizeof(dp));
    queue que;
    que.push(s);dp[s]=0;num[0]=1;
    while(!que.empty()){
        u=que.front();que.pop();
        for(i=head[u];i!=-1;i=next[i]){
            if(cap[i]>0) continue;
            if(dp[to[i]]>dp[u]+1){
                dp[to[i]]=dp[u]+1;
                que.push(to[i]);
                num[dp[to[i]]]++;
            }
        }
    }
}
int cur[N],pre[N];
int sap(int s,int e,int n){
    int i,x,ans=0,inf;
    bfs(e,s);x=s;
    memcpy(cur,head,sizeof(head));
    while(dp[s]0) break;
        }
        if(i!=-1){
            cur[x]=i;pre[to[i]]=i;x=to[i];
        }else {
            inf=n-1;
            if(--num[dp[x]]==0) break;
            for(i=head[x];i!=-1;i=next[i]){
                if(cap[i]>0&&dp[to[i]]=maxx) maxx=a,e=i;
        }
        for(i=1;i<=m;i++){
            scanf("%d%d%d",&a,&b,&c);add(a,b,c);add(b,a,c);
        }
        printf("%d\n",sap(s,e,n));
    }
    return 0;
}

你可能感兴趣的:(一些模板,图论)