解题报告:Q、运输问题(最大费用最大流 + 最小费用最大流)

在这里插入图片描述
【问题分析】

费用流问题。

【建模方法】

把所有仓库看做二分图中顶点Xi,所有零售商店看做二分图中顶点Yi,建立附加源S汇T。

1、从S向每个Xi连一条容量为仓库中货物数量ai,费用为0的有向边。
2、从每个Yi向T连一条容量为商店所需货物数量bi,费用为0的有向边。
3、从每个Xi向每个Yj连接一条容量为无穷大,费用为cij的有向边。

求最小费用最大流,最小费用流值就是最少运费,求最大费用最大流,最大费用流值就是最多运费。

【建模分析】

把每个仓库想象成一个中转站,由源点运来ai单位货物,运费为0,每个商店也为一个中转站,运向目标汇点bi单位货物。每个仓库和零售商店之间有一条道路,容量为无穷大,费用为单位运费cij。求从源点到汇点的费用流,就是运费。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>

using namespace std;
typedef long long ll;

const int N = 5e3 + 7, M = 2e6 + 7, INF = 0x3f3f3f3f;
int cost[N][N];
namespace dinic{//MCMF

    const int N = 1e5 + 7, M = 2e6 + 7;
    const ll INF = 0x3f3f3f3f3f;//!因为是long long 所以是五个3f
    int n, S, T;
    int head[N], ver[M], nex[M], tot, cur[N];
    ll dist[N], edge[M], cost[M], maxflow, mincost;
    bool vis[N];

    inline void add(int x, int y, ll z, ll c, bool o = 1){
        ver[tot] = y;
        edge[tot] = z;
        cost[tot] = c;
        nex[tot] = head[x];
        head[x] = tot ++ ;
        if(o)add(y, x, 0, -c, 0);
    }

    inline bool spfa(){//对费用 cost 求最短路
        //memset(dist, 0x3f, sizeof dist);//!0x3f3f3f3f3f -> 3f x 5
        for(int i = 1;i <= n; ++ i)dist[i] = INF;
        memset(vis, 0, sizeof vis);
        queue<int>q;
        q.push(S);
        dist[S] = 0;
        vis[S] = 1;
        while(q.size()){
            int x = q.front();
            q.pop();
            vis[x] = 0;
            for(int i = head[x]; ~i; i = nex[i]){
                int y = ver[i];
                ll z = edge[i], c = cost[i];
                if(dist[y] <= dist[x] + c || !z)continue;
                dist[y] = dist[x] + c;
                if(!vis[y])
                    q.push(y), vis[y] = 1;
            }
        }
        return dist[T] != INF;
    }


    ll dfs(int x, ll flow = INF){
        if(x == T)return flow;
        ll ans = 0, k, i;
        vis[x] = 1;
        for(i = cur[x]; ~i && flow; i = nex[i]){
            cur[x] = i;
            int y = ver[i];
            ll z = edge[i], c = cost[i];
            if(!z || (dist[y] != dist[x] + c) || vis[y])continue;
            k = dfs(y, min(flow, z));
            if(!k)dist[y] = INF;
            edge[i] -= k;
            edge[i ^ 1] += k;
            ans += k, mincost += k * c, flow -= k;
        }
        vis[x] = 0;
        return ans;
    }

    inline void main(){
        while(spfa()){
            for(int i = 1; i <= n; ++ i)
                cur[i] = head[i];
            //memcpy(cur, head, sizeof head);
            ll now;
            while((now = dfs(S)))maxflow += now;//!
        }
    }

    inline void init(int _n, int _S, int _T){
        n = _n, S = _S, T = _T, tot = 0, maxflow = 0, mincost = 0;
        memset(head, -1, sizeof head);
    }
}

int a[N], b[N];
int n, m, S, T;
int main(){
    scanf("%d%d", &n, &m);
    int S = 1, T = n + m + 2;

    dinic::init(n + m + 2, S, T);//

    for(int i = 1 ;i <= n; ++ i)
        scanf("%d", &a[i]);

    for(int i = 1 ;i <= m; ++ i)
        scanf("%d", &b[i]);

    for(int i = 1;i <= n; ++ i){
        dinic::add(S, i + 1, a[i], 0);
    }
    for(int i = 1;i <= m; ++ i){
        dinic::add(i + n + 1, T, b[i], 0);
    }

    for(int i = 1; i <= n; ++ i)
        for(int j = 1; j <= m; ++j){
            scanf("%d", &cost[i][j]);
        }

    for(int i = 1; i <= n;++ i)
    for(int j = 1; j<= m; ++ j){
        dinic::add(i + 1, j + n + 1, INF, cost[i][j]);
    }

    dinic::main();
    printf("%lld\n",dinic::mincost);

    dinic::init(n + m + 2, S, T);

    for(int i = 1;i <= n; ++ i){
        dinic::add(S, i + 1, a[i], 0);
    }
    for(int i = 1;i <= m; ++ i){
        dinic::add(i + n + 1, T, b[i], 0);
    }

    for(int i = 1; i <= n;++ i)
    for(int j = 1; j<= m; ++ j){
        dinic::add(i + 1, j + n + 1, INF, -cost[i][j]);
    }

    dinic::main();
    printf("%lld\n",-dinic::mincost);
    return 0;
}

注:本文部分题解来自 线性规划与网络流24题解题报告,个人留存学习使用

猜你喜欢

转载自blog.csdn.net/weixin_45697774/article/details/108502923
今日推荐