zoukankan      html  css  js  c++  java
  • 费用流模板

    inline void add(int x,int y,int f,int c){
        node[hcnt]=Node(y,head[x],f,c);head[x]=hcnt++;
        node[hcnt]=Node(x,head[y],0,-c);head[y]=hcnt++;
    }
    int spfa(){
        mst(d,-1);d[S]=0;
        mst(vis,0);q.push(S);
        mst(pre,-1);
        while(!q.empty()){
            int x=q.front();q.pop();
            vis[x]=0;
            for(int i=head[x];~i;i=node[i].next){
                int e=node[i].to;
                if(node[i].f&&d[e]<d[x]+node[i].c){
                    d[e]=d[x]+node[i].c;
                    pre[e]=i^1;
                    if(!vis[e]){
                        vis[e]=1;
                        q.push(e);
                    }
                }
            }
        }
        return pre[T]!=-1;
    }
    void work(){
        int fl=inf;
        for(int i=pre[T];~i;i=pre[node[i].to]){
            fl=min(fl,node[i^1].f);
        }
        for(int i=pre[T];~i;i=pre[node[i].to]){
            node[i].f+=fl;node[i^1].f-=fl;
           // ans+=node[i^1].c;
        }
        ans+=d[T]; ///ans是费用
    }
  • 相关阅读:
    刷题238. Product of Array Except Self
    刷题236. Lowest Common Ancestor of a Binary Tree
    刷题208. Implement Trie (Prefix Tree)
    A1070
    A1048
    A1050
    A1041
    A1092
    A1084
    n进制转十进制
  • 原文地址:https://www.cnblogs.com/Kurokey/p/5718851.html
Copyright © 2011-2022 走看看