poj 2516 Minimum Cost(最小费最大流)

poj 2516 Minimum Cost

Description
Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods (marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport.

It’s known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places’ storage of K kinds of goods, N shopkeepers’ order of K kinds of goods and the cost to transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.

Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, K (0 < N, M, K < 50), which are described above. The next N lines give the shopkeepers’ orders, with each line containing K integers (there integers are belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places’ storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in that supply place.

Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper.

The input is terminated with three “0”s. This test case should not be processed.

Output
For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output “-1”.

Sample Input

1 3 3
1 1 1
0 1 1
1 2 2
1 0 1
1 2 3
1 1 1
2 1 1

1 1 1
3
2
20

0 0 0

Sample Output

4
-1

题目大意:输入数据。

这里写图片描述

此图来自:ζёСяêτ - 小優YoU
在已知这些信息之后,求供货商能否满足商店的需求,不能输出“-1”,能的话输出供货商给商家发货的最小费用。
解题思路:一开是看完这道题目,觉得无从下手,这图太复杂了。后来才想到其实不一定只能用一张图,这题可以拆成k张图。首先要判断一下,供货商能否满足商家的货物需求,不能的话输出-1,能的话,继续建图。每张图设置一个超级源点连向所有的供货商,容量为该供货商第i种商品的存货 1<=i<=k ,费用为0;每个供货商都连向所有的商店,容量为INF,费用为该供货商给对应商店发送i号货物的花费;最后设置一个超级汇点,使所有商店连向该汇点,容量为该商店的需求,费用为0;。然后求k次最小费,把k次的最小费加起来,就是最终答案。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;

const int N = 500;
const int M = 50005;
const int OF = 60;
const int FIN = 400;
const int INF = 0x3f3f3f3f;
int shop[N][N], sup[N][N], good[N][N][N], rec[N];
typedef long long ll;
int n, m, k, s, t, flag;
int pre[N], inq[N]; 
ll a[N], d[N];

struct Edge{
    int from, to;
    ll cap, flow;
    ll cos;
};

vector<Edge> edges;
vector<int> G[M];

void init() {
    for (int i = 0; i < M; i++) G[i].clear();
    edges.clear();
}

void addEdge(int from, int to, ll cap, ll flow, ll cos) {
    edges.push_back((Edge){from, to, cap, 0, cos});
    edges.push_back((Edge){to, from, 0, 0, -cos});
    int m = edges.size();
    G[from].push_back(m - 2); G[to].push_back(m - 1);
}

void input() {
    memset(rec, 0, sizeof(rec));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < k; j++) {
            scanf("%d", &shop[i][j]);
            rec[j] += shop[i][j];
        }
    }   
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < k; j++) {
            scanf("%d", &sup[i][j]);    
            rec[j] -= sup[i][j];
        }   
    }
    for (int i = 0; i < k; i++) {
        if (rec[i] > 0) flag = 0;
    }
    for (int l = 0; l < k; l++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                scanf("%d", &good[i][j][l]);    
            }   
        }   
    }
}

int BF(int s, int t, ll& flow, ll& cost) {
    queue<int> Q;
    memset(inq, 0, sizeof(inq));
    memset(a, 0, sizeof(a));
    memset(pre, 0, sizeof(pre));
    for (int i = 0; i < N; i++) d[i] = INF;
    d[s] = 0;
    a[s] = INF;
    inq[s] = 1;
    int flag = 1;
    pre[s] = 0;
    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 &e = edges[G[u][i]];
            if (e.cap > e.flow && d[e.to] > d[u] + e.cos) {
                d[e.to] = d[u] + e.cos;
                a[e.to] = min(a[u], e.cap - e.flow);
                pre[e.to] = G[u][i];
                if (!inq[e.to]) {
                    inq[e.to] = 1;
                    Q.push(e.to);
                }
            }   
        }
        flag = 0;
    }
    if (d[t] == INF) return 0;
    flow += a[t];
    cost += (ll)d[t] * (ll)a[t];
    for (int u = t; u != s; u = edges[pre[u]].from) {
        edges[pre[u]].flow += a[t];
        edges[pre[u]^1].flow -= a[t];
    }
    return 1;
}

int MCMF(int s, int t, ll& cost) {
    ll flow = 0;
    cost = 0;       
    while (BF(s, t, flow, cost));
    return flow;
}

void solve() { //求k次最小费
    ll ans = 0;
    for (int K = 0; K < k; K++) {
        init(); 
        for (int i = 1; i <= m; i++) {
            addEdge(s, i, sup[i - 1][K], 0, 0);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                addEdge(j, i + OF, INF, 0, good[i - 1][j - 1][K]);  
            }   
        }
        for (int i = 1; i <= n; i++) {
            addEdge(i + OF, t, shop[i - 1][K], 0, 0);   
        }
        ll cost;
        MCMF(s, t, cost);
        ans += cost;
    }
    printf("%lld\n", ans);
}

int main() {
    while (scanf("%d %d %d", &n, &m, &k) == 3) {
        if (!n && !m && !k) break;
        flag = 1;
        s = 0, t = FIN;
        input();    
        if (!flag) {
            printf("-1\n"); 
            continue;
        }
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值