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

    理论:http://www.cnblogs.com/acha/p/6735037.html

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    
    using namespace std;
    
    #define N 5001
    #define M 50001
    
    const int inf=1e9;
    
    int n,m,src,decc;
    
    int tot=1;
    
    int front[N],to[M<<1],nxt[M<<1],from[M<<1];
    int val[M<<1],cap[M<<1];
    
    int max_flow,cost;
    
    int dis[N];
    bool vis[N];
    
    void read(int &x)
    {
        x=0; char c=getchar();
        while(!isdigit(c)) c=getchar();
        while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
    }
    
    void add(int u,int v,int w,int f)
    {
        to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; from[tot]=u; val[tot]=f; cap[tot]=w;
        to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; from[tot]=v; val[tot]=-f; cap[tot]=0;
    }
    
    int augment(int now,int flow)
    {
        vis[now]=true;
        if(now==decc)
        {
            cost+=-dis[src]*flow;
            max_flow+=flow;
            return flow;
        }
        int delta;
        for(int i=front[now];i;i=nxt[i])
        {
            if(cap[i] && !vis[to[i]] && dis[to[i]]==dis[now]+val[i])
            {
                delta=augment(to[i],min(flow,cap[i]));
                if(delta)
                {
                    cap[i]-=delta;
                    cap[i^1]+=delta;
                    return delta;
                }
            }
        }
        return 0;
    }
    
    bool retreat()
    {
        if(vis[decc]) return true;
        int mi=inf;
        for(int i=2;i<=tot;i++)
        {
            if(cap[i] && vis[from[i]] && !vis[to[i]])
                mi=min(mi,dis[from[i]]+val[i]-dis[to[i]]);
        }
        if(mi==inf) return false;
        for(int i=1;i<=n;++i)
            if(vis[i]) dis[i]-=mi;
        return true;
    }
    
    void zkw()
    {
        do
        {
            memset(vis,false,sizeof(vis));
            augment(src,inf);
        }while(retreat());
        cout<<max_flow<<' '<<cost;
    }
    
    int main()
    {
        read(n); read(m); read(src); read(decc);
        int u,v,w,f;
        while(m--)
        {
            read(u); read(v); read(w);read(f);
            add(u,v,w,f);
        }
        zkw();
    }
  • 相关阅读:
    Integer和Integer常量池
    Spring中常用的23中设计模式
    GitHub 简单教程
    IDEA 中用好 Lombok,撸码效率至少提升5倍!
    Intellij IDEA中Mybatis Mapper自动注入警告的6种解决方案
    ROS常用命令和VIM常用命令
    ROS运行
    VINS-Mono运行
    环境配置相关
    C89标准和C99标准C11标准的区别
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8081914.html
Copyright © 2011-2022 走看看