zoukankan      html  css  js  c++  java
  • P4015 运输问题 网络流问题

    题目描述

    WW 公司有 mm 个仓库和 nn 个零售商店。第 ii 个仓库有 a_iai 个单位的货物;第 jj 个零售商店需要 b_jbj 个单位的货物。

    货物供需平衡,即sumlimits_{i=1}^{m}a_i=sumlimits_{j=1}^{n}b_ji=1mai=j=1nbj

    从第 ii 个仓库运送每单位货物到第 jj 个零售商店的费用为 c_{ij}cij​​ 。

    试设计一个将仓库中所有货物运送到零售商店的运输方案,使总运输费用最少。

    输入输出格式

    输入格式:

    第 11 行有 22 个正整数 mm 和 nn,分别表示仓库数和零售商店数。

    接下来的一行中有 mm 个正整数 a_iai,表示第 ii 个仓库有 a_iai个单位的货物。

    再接下来的一行中有 nn 个正整数 b_jbj,表示第 jj 个零售商店需要 b_jbj 个单位的货物。

    接下来的 mm 行,每行有 nn 个整数,表示从第 ii 个仓库运送每单位货物到第 jj 个零售商店的费用 c_{ij}cij

    输出格式:

    两行分别输出最小运输费用和最大运输费用。

    输入输出样例

    输入样例#1: 复制
    2 3
    220 280
    170 120 210
    77 39 105
    150 186 122
    输出样例#1: 复制
    48500
    69140






    这个题目特别简单,就是一个裸题,不过我的写法复杂了一点。


    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <iostream>
    #include <map>
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    const int INF = 0x3f3f3f3f;
    const int maxn = 2e5 + 10;
    struct edge
    {
        int u, v, c, f, cost;
        edge(int u, int v, int c, int f, int cost) :u(u), v(v), c(c), f(f), cost(cost) {}
    };
    vector<edge>e;
    vector<int>G[maxn];
    int a[maxn];//找增广路每个点的水流量
    int p[maxn];//每次找增广路反向记录路径
    int d[maxn];//SPFA算法的最短路
    int inq[maxn];//SPFA算法是否在队列中
    int s, t, exa[maxn];
    void init()
    {
        for (int i = 0; i <= maxn; i++)G[i].clear();
        e.clear();
    }
    void add(int u, int v, int c, int cost)
    {
        e.push_back(edge(u, v, c, 0, cost));
        e.push_back(edge(v, u, 0, 0, -cost));
        //printf("%d %d %d %d
    ", u, v, c, cost);
        int m = e.size();
        G[u].push_back(m - 2);
        G[v].push_back(m - 1);
    }
    bool bellman(int s, int t, int& flow, ll &cost)
    {
        memset(d, 0xef, sizeof(d));
        memset(inq, 0, sizeof(inq));
        d[s] = 0; inq[s] = 1;//源点s的距离设为0,标记入队
        p[s] = 0; a[s] = INF;//源点流量为INF(和之前的最大流算法是一样的)
    
        queue<int>q;//Bellman算法和增广路算法同步进行,沿着最短路拓展增广路,得出的解一定是最小费用最大流
        q.push(s);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            inq[u] = 0;//入队列标记删除
            for (int i = 0; i < G[u].size(); i++)
            {
                edge & now = e[G[u][i]];
                int v = now.v;
                if (now.c > now.f && d[v] < d[u] + now.cost)
                    //now.c > now.f表示这条路还未流满(和最大流一样)
                    //d[v] > d[u] + e.cost Bellman 算法中边的松弛
                {
                    // printf("d[%d]=%d d[%d]=%d %d d[%d]=%d
    ", v,d[v],u, d[u], now.cost,v,d[u]+now.cost);
                    // printf("%d %d %d %d %d %d
    ", u, now.u, now.v, now.c, now.f, now.cost);
                    d[v] = d[u] + now.cost;//Bellman 算法边的松弛
                    p[v] = G[u][i];//反向记录边的编号
                    a[v] = min(a[u], now.c - now.f);//到达v点的水量取决于边剩余的容量和u点的水量
                    if (!inq[v]) { q.push(v); inq[v] = 1; }//Bellman 算法入队
                }
            }
        }
        // printf("a=%d d=%d
    ", a[t], d[t]);
        if (d[t] < 0)return false;//找不到增广路
        flow += a[t];//最大流的值,此函数引用flow这个值,最后可以直接求出flow
        cost += 1ll * d[t] * 1ll * a[t];//距离乘上到达汇点的流量就是费用
        // printf("cost=%lld
    ", cost);
        for (int u = t; u != s; u = e[p[u]].u)//逆向存边
        {
            e[p[u]].f += a[t];//正向边加上流量
            e[p[u] ^ 1].f -= a[t];//反向边减去流量 (和增广路算法一样)
        }
        return true;
    }
    int Maxflow(int s, int t, ll & cost)
    {
        memset(p, 0, sizeof(p));
        cost = 0;
        int flow = 0;
        while (bellman(s, t, flow, cost));//由于Bellman函数用的是引用,所以只要一直调用就可以求出flow和cost
        return flow;//返回最大流,cost引用可以直接返回最小费用
    }
    
    bool bellman1(int s, int t, int& flow, long long & cost)
    {
        memset(d, inf, sizeof(d));
        memset(inq, 0, sizeof(inq));
        d[s] = 0; inq[s] = 1;//源点s的距离设为0,标记入队
        p[s] = 0; a[s] = INF;//源点流量为INF(和之前的最大流算法是一样的)
    
        queue<int>q;//Bellman算法和增广路算法同步进行,沿着最短路拓展增广路,得出的解一定是最小费用最大流
        q.push(s);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            inq[u] = 0;//入队列标记删除
            for (int i = 0; i < G[u].size(); i++)
            {
                edge & now = e[G[u][i]];
                int v = now.v;
                if (now.c > now.f && d[v] > d[u] + now.cost)
                    //now.c > now.f表示这条路还未流满(和最大流一样)
                    //d[v] > d[u] + e.cost Bellman 算法中边的松弛
                {
                    d[v] = d[u] + now.cost;//Bellman 算法边的松弛
                    p[v] = G[u][i];//反向记录边的编号
                    a[v] = min(a[u], now.c - now.f);//到达v点的水量取决于边剩余的容量和u点的水量
                    if (!inq[v]) { q.push(v); inq[v] = 1; }//Bellman 算法入队
                }
            }
        }
        if (d[t] == INF)return false;//找不到增广路
        flow += a[t];//最大流的值,此函数引用flow这个值,最后可以直接求出flow
        cost += (long long)d[t] * (long long)a[t];//距离乘上到达汇点的流量就是费用
        for (int u = t; u != s; u = e[p[u]].u)//逆向存边
        {
            e[p[u]].f += a[t];//正向边加上流量
            e[p[u] ^ 1].f -= a[t];//反向边减去流量 (和增广路算法一样)
        }
        return true;
    }
    int Minflow(int s, int t, long long & cost)
    {
        memset(p, 0, sizeof(p));
        cost = 0;
        int flow = 0;
        while (bellman1(s, t, flow, cost));//由于Bellman函数用的是引用,所以只要一直调用就可以求出flow和cost
        return flow;//返回最大流,cost引用可以直接返回最小费用
    }
    int qa[110], qb[110];
    int qc[110][110];
    int main()
    {
        int n, m;
        cin >> n >> m;
        s = 0, t = n + m + 1;
        for (int i = 1; i <= n; i++)
        {
            cin >> qa[i];
            add(s, i, qa[i], 0);
        }
        for (int i = 1; i <= m; i++)
        {
            cin >> qb[i];
            add(i + n, t, qb[i], 0);
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                cin >> qc[i][j];
                add(i, j + n, inf, qc[i][j]);
            }
        }
        ll cost = 0;
        int ans = Minflow(s, t, cost);
        printf("%lld
    ", cost);
        init();
        for (int i = 1; i <= n; i++) add(s, i, qa[i], 0);
        for (int i = 1; i <= m; i++) add(i + n, t, qb[i], 0);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
                add(i, j + n, inf, qc[i][j]);
        }
        cost = 0;
        ans = Maxflow(s, t, cost);
        printf("%lld
    ", cost);
        return 0;
    }





  • 相关阅读:
    HDU 1698 Just a Hook (区间更新+延迟标记)
    HDU 1754 I Hate It 线段树
    HDU 1847 Good Luck in CET-4 Everybody! (sg函数)
    博弈汇总
    Codeforces cf 713A Sonya and Queries
    hihoCoder 1082 : 然而沼跃鱼早就看穿了一切
    hihoCoder 1298 : 数论五·欧拉函数
    hdu 5821 Ball
    hdu 5818 Joint Stacks(栈的模拟)
    hdu 5802 Windows 10
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10786829.html
Copyright © 2011-2022 走看看