zoukankan      html  css  js  c++  java
  • P3381 【模板】最小费用最大流

    link

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <unordered_set>
    #include <cstring>
    #include <queue>
    # define LL long long
    using namespace std;
    
    const int INF=0x7fffffff;
    const int maxn=5000+10;
    const int maxm=50000+10;
    int N,M,S,T;
    int res1;
    int res2;
    int head[maxn];
    int en;
    struct Edge{
        int to;
        int next;
        int c;
        int fi;
    }e[maxm<<1];
    
    int dis[maxn];
    int preedge[maxn];
    int minflow[maxn];
    int inque[maxn];
    
    void addEdge(int from, int to, int capa, int f){
        e[en].next=head[from];
        e[en].to=to;
        e[en].c=capa;
        e[en].fi=f;
        head[from]=en;
        ++en;
    }
    
    void add(int from, int to, int capa, int f){
        addEdge(from,to,capa,f);
        addEdge(to,from,0,-f);
    }
    
    void spfa(){
        memset(preedge,-1,sizeof(preedge));
        memset(minflow,0,sizeof(minflow));
        memset(dis,0x3f, sizeof(dis));
        memset(inque,0,sizeof(inque));
    
        queue<int> q;
        q.push(S);
        dis[S]=0;
        minflow[S]=INF;
        inque[S]=1;
        while(!q.empty()){
            int cur=q.front();
            q.pop();
            inque[cur]=0;
            for(int i=head[cur];i!=-1;i=e[i].next){
                int v=e[i].to;
                if(e[i].c>0 && dis[v]>dis[cur]+e[i].fi){
                    dis[v]=dis[cur]+e[i].fi;
                    preedge[v]=i;
                    minflow[v]=min(minflow[cur],e[i].c);
                    if(inque[v]==0){
                        q.push(v);
                        inque[v]=1;
                    }
                }
            }
        }
    }
    
    void EK(){
        while(true){
            spfa();
            if(preedge[T]==-1) break;
            res1+=minflow[T];
            res2+=minflow[T]*dis[T];
            int v=T;
            while(true){
                int edge=preedge[v];
                int u=e[edge^1].to;
                e[edge].c-=minflow[T];
                e[edge^1].c+=minflow[T];
                if(u==S) break;
                v=u;
            }
        }
    }
    
    int main(){
        memset(head,-1,sizeof(head));
        scanf("%d %d %d %d", &N, &M, &S, &T);
        while(M--){
            int u,v;
            int c,f;
            scanf("%d %d %d %d", &u, &v, &c, &f);
            add(u,v,c,f);
        }
        EK();
        printf("%d %d", res1, res2);
        return 0;
    }
  • 相关阅读:
    ERP系统模块完全解析──主生产计划MPS
    样式兼容问题
    Js中 关于top、clientTop、scrollTop、offsetTop
    C# 中的委托和事件
    面试题大全
    常用Web服务
    CSS兼容IE6,IE7,FF的技巧
    C#图片处理基本应用(裁剪,缩放,清晰度,水印)
    数据库导入excel数据出现问题解决方案
    JS调用webservice的通用函数
  • 原文地址:https://www.cnblogs.com/FEIIEF/p/12771426.html
Copyright © 2011-2022 走看看