• 最小费用流


    最小费用流
    这是一道模板题。

    给定一个图,每条边有容量和费用,使用每条边的单位流量需要支付特定的费用。给定源点 1和汇点 n,求图的最大流和最大流需要支付的最小费用。

    输入格式:
    第一行两个整数n,m,表示有 n 个点 m 条边。

    从第二行开始的之后 m 行,每行四个整数s​i​​ ,t​i​​ ,c​i ,w​i 表示一条从 s​i到 t​i​​ 的边,容量为c​i,单位流量需要支付的费用为w​i​​ 。
    数据保证有1≤n≤400,0≤m≤15000,w​i ≥0,保证输入数据、中间结果以及答案在 32 位有符号整数范围内。

    输出格式:
    一行两个整数,分别表示最大流和最大流需要支付的最小费用。

    输入样例:
    在这里给出一组输入。例如:

    
    8 23
    2 3 2147483647 1
    1 3 1 1
    2 4 2147483647 2
    1 4 1 2
    2 8 2 0
    3 5 2147483647 3
    1 5 1 3
    3 6 2147483647 4
    1 6 1 4
    3 8 2 0
    3 2 2147483647 0
    4 6 2147483647 5
    1 6 1 5
    4 7 2147483647 6
    1 7 1 6
    4 8 2 0
    4 2 2147483647 0
    5 8 0 0
    5 2 2147483647 0
    6 8 0 0
    6 2 2147483647 0
    7 8 0 0
    7 2 2147483647 0
    

    输出样例:
    在这里给出相应的输出。例如:

    6 24

    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    const int maxn = 1e4+10;
    const int maxm = 1e5+10;
    const int inf = 0x3f3f3f3f;
    struct Edge
    {
        int to,next,cap,flow,cost;
    }edge[maxm];
    int head[maxn],tol,pre[maxn],dis[maxn],N;
    bool vis[maxn];
    void Init(int n)
    {
        N = n;
        tol = 0;
        memset(head,-1, sizeof(head));
    }
    void add_edge(int u,int v,int cap,int cost)
    {
        edge[tol].to = v;
        edge[tol].cap = cap;
        edge[tol].cost = cost;
        edge[tol].flow = 0;
        edge[tol].next = head[u];
        head[u] = tol++;
    
        edge[tol].to = u;
        edge[tol].cap = 0;
        edge[tol].cost = -cost;
        edge[tol].flow = 0;
        edge[tol].next = head[v];
        head[v] = tol++;
    }
    bool Spfa(int s,int t)
    {
        queue<int>q;
        while(!q.empty())
            q.pop();
        for(int i = 0;i <= N; i++)
        {
            dis[i] = inf;
            vis[i] = false;
            pre[i] = -1;
        }
        dis[s] = 0;
        vis[s] = true;
        q.push(s);
        while(!q.empty())
        {
            int u = q.front();
            q.pop();
            vis[u] = false;
            for(int i = head[u];i != -1;i = edge[i].next)
            {
                int v = edge[i].to;
                if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost)
                {
                    dis[v] = dis[u] + edge[i].cost;
                    pre[v] = i;
                    if(!vis[v])
                    {
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        if(pre[t] == -1)
            return false;
        return true;
    }
    void minCostmaxFlow(int s,int t)
    {
        int flow = 0,cost = 0;
        while(Spfa(s,t))
        {
            int Min = inf;
            for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
            {
                if(Min > edge[i].cap - edge[i].flow)
                    Min = edge[i].cap - edge[i].flow;
            }
            for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
            {
                edge[i].flow += Min;
                edge[i^1].flow -= Min;
                cost += edge[i].cost * Min;
            }
            flow += Min;
        }
        cout << flow << " " << cost << endl;
    }
    signed main()
    {
        //freopen("in","r",stdin);
        ios::sync_with_stdio(false);
        cin.tie(0);
        int n,m,s,t,c,w;
        cin >> n >> m;
        Init(n);
        for(int i = 0;i < m; i++)
        {
            cin >> s >> t >> c >> w;
            add_edge(s,t,c,w);
        }
        minCostmaxFlow(1,n);
        return 0;
    }
    
    
    
  • 相关阅读:
    Python 操作 MySQL数据库提示pymysql.err.InternalError: (1054, "Unknown column 'XXXXXXXXX' in 'where clause'")解决方法
    MySQL连接池不能查到刚写入的数据——连接池配置问题
    python 将字典转为bytes类型字典
    关于状态机的问题思考——什么时候达到新的状态?什么时候清除老状态?新状态与老状态之间的关系
    mysql 8.0.19 安装 及 端口修改
    sprintf printf 输出数据固定格式——数字前补零
    思维大爆炸
    IO点作为状态判断——一定要做软件“消抖”
    React-umi-request动态刷新Token功能实现及node.js 代码逻辑
    js测试题
  • 原文地址:https://www.cnblogs.com/xcfxcf/p/12301648.html
Copyright © 2020-2023  润新知