网络流24题17. 运输问题

运输问题

Description

W 公司有 m 个仓库和 n 个零售商店。第 i 个仓库有 ai 个单位的货物;第 j 个零售商店需要 bj 个单位的货物。货物供需平衡,即

Σai=Σbj

从第 i 个仓库运送每单位货物到第 j 个零售商店的费用为 cij 。试设计一个将仓库中所有货物运送到零售商店的运输方案,使总运输费用最少。
对于给定的 m 个仓库和 n 个零售商店间运送货物的费用,计算最优运输方案和最差运输方案。

Input

第 1 行有 2 个正整数 m和 n,分别表示仓库数和零售商店数。接下来的一行中有 m个正整数 ai ,1≤i≤m,表示第 i 个仓库有 ai 个单位的货物。再接下来的一行中有 n 个正整数 bj ,1≤j≤n,表示第 j 个零售商店需要 bj 个单位的货物。接下来的 m行,每行有 n 个整数,表示从第 i 个仓库运送每单位货物到第 j 个零售商店的费用 cij

Output

输出计算出的最少运输费用和最多运输费用。

Sample Input

2 3
220 280
170 120 210
77 39 105
150 186 122

Sample Output

48500
69140

题解

建图:
建立附加源S,附加汇T。
1.S向每个仓库连接一条容量为仓库货物数量,费用为0的边。
2.每个商店向T连接一条容量为商店需要货物数量,费用为0的边。
3.每个仓库和每个商店之间连接一条容量无穷大,费用为运送费用的边。
求出最小(大)费用最大流就是答案。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 1000 + 10, M = 1000000 + 10, inf = 0x3f3f3f3f;

struct Edge{
    int fr, to, cap, flow, cost;
}edg[M];
int nxt[M], hd[N], tot;
int n, m;
int s, t;
int q[N], inq[N], p[N], a[N], d[N];
int wh[N], sp[N], val[N][N];
void insert(int u, int v, int w, int x){
    edg[tot].fr = u, edg[tot].to = v, edg[tot].cap = w, edg[tot].flow = 0, edg[tot].cost = x;
    nxt[tot] = hd[u]; hd[u] = tot;
    tot++;
    edg[tot].fr = v, edg[tot].to = u, edg[tot].cap = 0, edg[tot].flow = 0, edg[tot].cost = -x;
    nxt[tot] = hd[v]; hd[v] = tot;
    tot++;
}
void init(){
    scanf("%d%d", &m, &n);
    s = 0, t = n + m + 1;
    int w;
    for(int i = 1; i <= m; i++)
        scanf("%d", &wh[i]);
    for(int i = 1; i <= n; i++)
        scanf("%d", &sp[i]);
    for(int i = 1; i <= m; i++)
    for(int j = 1; j <= n; j++)
        scanf("%d", &val[i][j]);
}
void build(){
    tot = 0;
    memset(hd, -1, sizeof(hd));
    for(int i = 1; i <= m; i++)
        insert(s, i, wh[i], 0);
    for(int i = 1; i <= n; i++)
        insert(i + m, t, sp[i], 0);
    for(int i = 1; i <= m; i++)
    for(int j = 1; j <= n; j++)
        insert(i, m + j, inf, val[i][j]);
}
bool spfa1(int &fl, int &cst){
    for(int i = s; i <= t; i++) d[i] = inf;
    p[s] = 0; a[s] = inf; d[s] = 0;
    int head = 0, tail = 1;
    q[0] = s; inq[s] = 1;
    while(head != tail){
        int u = q[head++]; if(head == 1001) head = 0;
        inq[u] = 0;
        for(int i = hd[u]; i >= 0; i = nxt[i]){
            Edge &e = edg[i];
            if(d[e.to] > d[u] + e.cost && e.cap > e.flow){
                d[e.to] = d[u] + e.cost;
                p[e.to] = i;
                a[e.to] = min(a[u], e.cap - e.flow);
                if(!inq[e.to]){
                    q[tail++] = e.to; if(tail == 1001) tail = 0;
                    inq[e.to] = 1;
                }
            }
        }
    }
    if(d[t] == inf) return false;
    fl += a[t];
    cst += a[t] * d[t];
    int u = t;
    while(u != s){
        edg[p[u]].flow += a[t];
        edg[p[u]^1].flow -= a[t];
        u = edg[p[u]].fr;
    }
    return true;
}
bool spfa2(int &fl, int &cst){
    for(int i = s; i <= t; i++) d[i] = -inf;
    p[s] = 0; a[s] = inf; d[s] = 0;
    int head = 0, tail = 1;
    q[0] = s; inq[s] = 1;
    while(head != tail){
        int u = q[head++]; if(head == 1001) head = 0;
        inq[u] = 0;
        for(int i = hd[u]; i >= 0; i = nxt[i]){
            Edge &e = edg[i];
            if(d[e.to] < d[u] + e.cost && e.cap > e.flow){
                d[e.to] = d[u] + e.cost;
                p[e.to] = i;
                a[e.to] = min(a[u], e.cap - e.flow);
                if(!inq[e.to]){
                    q[tail++] = e.to; if(tail == 1001) tail = 0;
                    inq[e.to] = 1;
                }
            }
        }
    }
    if(d[t] == -inf) return false;
    fl += a[t];
    cst += a[t] * d[t];
    int u = t;
    while(u != s){
        edg[p[u]].flow += a[t];
        edg[p[u]^1].flow -= a[t];
        u = edg[p[u]].fr;
    }
    return true;
}
void work(){
    int flow = 0, cost = 0;
    build();
    while(spfa1(flow, cost));
    printf("%d\n", cost);
    flow = cost = 0;
    build();
    while(spfa2(flow, cost));
    printf("%d\n", cost);
}
int main(){
    freopen("prog817.in", "r", stdin);
    freopen("prog817.out", "w", stdout);
    init();
    work();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值