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

    最小费用流:在网络中为每条边加上一个费用,当流量固定为F时费用的最小值。
    模板一:通过Bellman-Ford算法计算最短路,并且沿着最短路增广。
        时间复杂度为(O(F|V||E|))

    const int maxn=1010,inf=0x3f3f3f3f;
    struct edge{
        int to,cap,cost,rev;
    };
    
    int n,dist[maxn],prevv[maxn],preve[maxn];
    vector<edge> g[maxn];
    
    void add_edge(int from,int to,int cap,int cost){
        g[from].push_back((edge){to,cap,cost,g[to].size()});
        g[to].push_back((edge){from,0,-cost,g[from].size()-1});
    }
    
    int min_cost_flow(int s,int t,int f){
        int res=0;
        while(f>0){
            memset(dist,0x3f,sizeof(dist));
            dist[s]=0;
            bool update=true;
            while(update){
                update=false;
                for(int v=0;v<n;v++){
                    if(dist[v]==inf) continue;
                    int l=g[v].size();
                    for(int i=0;i<l;i++){
                        edge &e=g[v][i];
                        if(e.cap>0 && dist[e.to]>dist[v]+e.cost){
                            dist[e.to]=dist[v]+e.cost;
                            prevv[e.to]=v;
                            preve[e.to]=i;
                            update=true;
                        }
                    }
                }
            }
            if(dist[t]==inf) return -1;
            int d=f;
            for(int v=t;v!=s;v=prevv[v]){
                d=min(d,g[prevv[v]][preve[v]].cap);
            }
            f-=d;
            res+=d*dist[t];
            for(int v=t;v!=s;v=prevv[v]){
                edge &e=g[prevv[v]][preve[v]];
                e.cap-=d;
                g[v][e.rev].cap+=d;
            }
        }
        return res;
    }
    

    模板二:通过Dijkstra算法计算最短路,并且沿着最短路增广。
        时间复杂度为(O(F|E|log|V|))

    #define PII pair<int,int>
    
    const int maxn=1010,inf=0x3f3f3f3f;
    struct edge{
        int to,cap,cost,rev;
    };
    
    int n,h[maxn],dist[maxn],prevv[maxn],preve[maxn];
    vector<edge> g[maxn];
    
    void add_edge(int from,int to,int cap,int cost){
        g[from].push_back((edge){to,cap,cost,g[to].size()});
        g[to].push_back((edge){from,0,-cost,g[from].size()-1});
    }
    
    int min_cost_flow(int s,int t,int f){
        int res=0;
        memset(h,0,sizeof(h));
        while(f>0){
            priority_queue<PII,vector<PII>,greater<PII> > q;
            memset(dist,0x3f,sizeof(dist));
            dist[s]=0;
            q.push(PII(0,s));
            while(!q.empty()){
                PII p=q.top();
                q.pop();
                int v=p.second;
                if(dist[v]<p.first) continue;
                int l=g[v].size();
                for(int i=0;i<l;i++){
                    edge &e=g[v][i];
                    if(e.cap>0 && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
                        dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
                        prevv[e.to]=v;
                        preve[e.to]=i;
                        q.push(PII{dist[e.to],e.to});
                    }
                }
            }
            if(dist[t]==inf) return -1;
            for(int v=0;v<n;v++) h[v]+=dist[v];  
            int d=f;
            for(int v=t;v!=s;v=prevv[v]){
                d=min(d,g[prevv[v]][preve[v]].cap);
            }
            f-=d;
            res+=d*h[t];
            for(int v=t;v!=s;v=prevv[v]){
                edge &e=g[prevv[v]][preve[v]];
                e.cap-=d;
                g[v][e.rev].cap+=d;
            }
        }
        return res;
    }
    
  • 相关阅读:
    startx
    BIOS above 4g setting
    How to download file from google drive
    凤凰财经
    SOTA state-of-the-art model
    update gpu drivers
    /opt/nvidia/deepstream/deepstream/sources/libs/nvdsinfer_customparser/nvdsinfer_custombboxparser.cpp
    retinanet keras
    fp16 fp32 int8
    tf.gather with tf.Session() as sess:
  • 原文地址:https://www.cnblogs.com/fxq1304/p/13182509.html
Copyright © 2011-2022 走看看