[最小割]hdoj 2435:There is a war

大致题意:
    给出一个有向带权图,设1为源点,n为汇点。现在要在2~n-1的点中加上一条容量为无穷的边使得这个图的最小割最大。求加上这条边后最小割最大是多少。

 

大致思路:
    先对原图求一遍最小割,割值为maxflow,在残余网络中分别找到两个点a,b,使得从1点到a的最大流值最大,为flowA,从b点到n点的最大流最大,为flowB。然后用maxflow+min(flowA,flowB)得到的就是答案。

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
class node{
    public:
    int u,v,next;
    int c;
    int cnm;
};node edge[200050];
int ne, head[1000];
int cur[1000], ps[1000], dep[1000],n,m,ans,cnts,cntt;
bool f[1000],ff[1000];
const int inf=90000000;
void addedge(int u, int v,int c){    // dinic的加边,还是有点不同的。
    edge[ne].u = u;
    edge[ne].v = v;
    edge[ne].c = c;
    edge[ne].next = head[u];
    edge[ne].cnm=ne+1;
    head[u] = ne ++;
    edge[ne].u = v;
    edge[ne].v = u;
    edge[ne].c =0;
    edge[ne].next = head[v];
    edge[ne].cnm=ne-1;
    head[v] = ne ++;
}

int dinic(int s, int t){     // dinic模板:源点为s,汇点为t
    int tr, res = 0;
    int i, j, k, f, r, top;
    while(1){
        memset(dep, -1, sizeof(dep));
        for(f = dep[ps[0]=s] = 0, r = 1; f != r;)
            for(i = ps[f ++], j = head[i]; j; j = edge[j].next)
                if(edge[j].c && dep[k=edge[j].v] == -1){
                    dep[k] = dep[i] + 1;
                    ps[r ++] = k;
                    if(k == t){
                        f = r; break;
                    }
                }
        if(dep[t] == -1) break;
        memcpy(cur, head, sizeof(cur));
        i = s, top = 0;
        while(1){
            if(i == t){
                for(tr = inf, k = 0; k < top; k ++)
                    if(edge[ps[k]].c < tr)
                        tr = edge[ps[f=k]].c;
                for(k = 0; k < top; k ++){
                    edge[ps[k]].c -= tr;
                    edge[ps[k]^1].c += tr;
                }
                i = edge[ps[top=f]].u;
                res += tr;          //
            }
            for(j = cur[i]; cur[i]; j = cur[i] = edge[cur[i]].next)
                if(edge[j].c && dep[i]+1 == dep[edge[j].v]) break;
            if(cur[i]){
                ps[top ++] = cur[i];
                i = edge[cur[i]].v;   //
            }else{
                if(top == 0) break;
                dep[i] = -1;
                i = edge[ps[-- top]].u;
            }
        }
    }
    return res;
}
void dfs1(int v){
 //   cout<<"v1 "<<v<<endl;
    f[v] = 1;
    cnts++;
    for(int i = head[v]; i != 0; i = edge[i].next){
        int vs=edge[i].v;
        if(f[vs]==0&&edge[i].c)
            dfs1(vs);
    }
}
void dfs2(int v){
  //  cout<<"v2 "<<v<<endl;
    ff[v] = 1;
    cntt++;
    for(int i = head[v]; i != 0; i =edge[i].next){
        int vs=edge[i].v;
        if(ff[vs]==0&&edge[edge[i].cnm].c)
            dfs2(vs);
    }
}
int aaa[100001];
int main(){
    int i,j,cas,a,b,c,maxflow;
    scanf("%d",&cas);
    while(cas--){
        ne=2;
        maxflow=0;
        cnts=cntt=0;
        memset(head,0,sizeof(head));
        scanf("%d%d",&n,&m);
        while(m--){
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c);
        }
        maxflow=dinic(1,n);
        memset(f,0,sizeof(f));
        memset(ff,0,sizeof(ff));
        dfs1(1);
        dfs2(n);
        int a1=0,a2=0;
        for(i=2;i<ne;i++){
            aaa[i]=edge[i].c;
         //   cout<<i<<" edge "<<edge[i].c<<endl;
        }
        for(i=2;i<n;i++){
            if(f[i]){
                for(j=2;j<ne;j++){
                    edge[j].c=aaa[j];
              //      cout<<j<<" edge "<<edge[j].c<<endl;
                }
                a1=max(a1,dinic(1,i));
             //   cout<<"a1 "<<a1<<endl;
            }
            if(ff[i]){
                for(j=2;j<ne;j++){
                    edge[j].c=aaa[j];
              //      cout<<j<<" edge "<<edge[j].c<<endl;
                }
                a2=max(a2,dinic(i,n));
             //   cout<<"a2 "<<a2<<endl;
            }
        }
        cout<<maxflow+min(a1,a2)<<endl;
    }
    return 0;
}
 

你可能感兴趣的:(数据结构,ACM,图论,最小割,hdoj 2435)