模板:最小割边数

做一遍最大流,满流设为1,其他为inf,再做一次最大流。

注意方向边流量不清零。

const int inf =  0x3f3f3f3f;
const int maxn = 200 + 5;
const int maxs = 3000 + 5;
int n,m;

struct Edge{
    int from,to,cap,flow;
};

struct Dicnic{
    int n,m,s,t;
    int d[maxs],cur[maxs],vis[maxs];
    vector<int>G[maxs];
    vector<Edge>edges;
    void AddEdge(int from,int to,int cap){
        Edge e1 = {from,to,cap,0};
        Edge e2 = {to,from,0,0};
        edges.push_back(e1);
        edges.push_back(e2);
        int m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    void init(int n){
        this -> n = n;
        for(int i = 0;i<=n;i++){
            G[i].clear();
        }
        edges.clear();
    }
    bool BFS(){     //就是用BFS分了个层而已,一看代码便知
        mem(vis,0);
        queue<int>Q;
        Q.push(s);
        vis[s] = 1;
        d[s] = 0;
        while(!Q.empty()){
            int x = Q.front();      Q.pop();
            for(int i=0;i<G[x].size();i++){
                Edge &e = edges[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow){
                    vis[e.to] = 1;
                    d[e.to] = d[x] + 1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int DFS(int x ,int a){  //就是顺着层次dfs而已
        if(x==t||a==0)      return a;
        int flow = 0 , f;
        for(int &i = cur[x];i<G[x].size();i++){
            Edge &e = edges[G[x][i]];
            if(d[e.to]==d[x]+1&&(f = DFS(e.to,min(a,e.cap-e.flow)))>0){  //a是路径上的最小残存容量
                e.flow += f;
                edges[G[x][i]^1].flow -= f;  //反向边
                flow += f;
                a -= f;
                if(a==0) break;   //a等于0及时退出,再从s那找一遍,因为从哪断的不知道
            }
        }
        return flow;
    }
    int MaxFlow(int s,int t){
        this -> s = s;      this ->t  = t;
        int flow = 0;
        while(BFS()){
            mem(cur,0);
            flow += DFS(s,inf);
        }
        return flow;
    }
    void build2(){
        for(int i=0;i<edges.size();i+=2){
            Edge &e=edges[i];
            if(e.flow==e.cap){
                e.cap=1;
                edges[i^1].cap=0;
            }
            else{
                e.cap=inf;
                edges[i^1].cap=0;
            }
            e.flow=0;
            //edges[i^1].flow=0;   //hdu 6214中反向边不清零就过了。。。
        }
    }
}g;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值