• 最小费用最大流 模板


    nm无影响   s为源点  t为汇点

    最大流为maxflow  最小费用为 mincost

    #include<bits/stdc++.h>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);--i)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define ll long long
    #define pb push_back
    #define inf 0x3f3f3f3f
    #define CLR(A,v)  memset(A,v,sizeof A)
    //////////////////////////////////
    const int N=100001;
    
    int n,m,S,T,k,maxflow,mincost,last[N],pre[N],dis[N],flow[N];
    bool vis[N];
    struct Edge{
        int next,to,flow,dis;
    }edge[N<<1];
    int pos=1,head[N];
    void init()
    {
        pos=1;
        CLR(head,0);
        mincost=maxflow=0;
    }
    queue <int> q;
    int id(int x,int y) {return n*(x-1)+y;}
    
    void add(int from,int to,int flow,int dis)//flow流量 dis费用
    {
        edge[++pos].next=head[from];
        edge[pos].flow=flow;
        edge[pos].dis=dis;
        edge[pos].to=to;
        head[from]=pos;
    
        edge[++pos].next=head[to];
        edge[pos].flow=0;
        edge[pos].dis=-dis;
        edge[pos].to=from;
        head[to]=pos;
    
    }
    bool spfa(int s,int t)
    {
        CLR(dis,0x3f);
        CLR(flow,0x3f);
        CLR(vis,0);
        while (!q.empty()) q.pop();
        dis[s]=0; pre[t]=-1; q.push(s); vis[s]=1;
        int tot=0;
        while (!q.empty())
        {
            int now=q.front(); q.pop(); vis[now]=0;
            for (int i=head[now]; i; i=edge[i].next)
            {
                int to=edge[i].to;
                if  (edge[i].flow>0 && dis[to]>dis[now]+edge[i].dis)
                {
                    dis[to]=edge[i].dis+dis[now];
                    flow[to]=min(edge[i].flow,flow[now]);
                    last[to]=i;
                    pre[to]=now;
                    if (!vis[to])
                    {
                        q.push(to); vis[to]=1;
                    }
                }
            }
        }
        return pre[t]!=-1;
    }
    void MCMF(int s,int t)
    {
        while (spfa(s,t))
        {
            int now=t;
            maxflow+=flow[t];
            mincost+=flow[t]*dis[t];
            while (now!=s)
            {
                edge[last[now]].flow-=flow[t];//dis . flow
                edge[last[now]^1].flow+=flow[t];
                now=pre[now];
            }
        }
    }
    View Code
  • 相关阅读:
    工坊第五天
    工坊第四天
    工坊第三天
    工坊第二天
    工坊第一天
    莫队 优雅暴力出奇迹
    状压 DP 总结
    关于MatlabGUI清除WorkSpace的用法
    ArduinoNano卡在上传,无法烧录
    两轮差速驱动机器人的坐标轨迹计算
  • 原文地址:https://www.cnblogs.com/bxd123/p/10927111.html
Copyright © 2020-2023  润新知