POJ - 2516 Minimum Cost (最小费用)

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

题意:

 n 个商店,每个商店有 k 个商品需求; m 个供应商,每个供应商供应给定个数的 k 个商品;给出商店在不同供应商采购不同商品的价格,求所有商店总的最小花费。

思路:

对每个商品都跑一次spfa求最小费用,最后加起来。

对第 k 个商品求MCMF,共 n + m + 2 个点。超级源点与供应商连边,流量为第 k 个商品的供应量,费用为0;供应商与商店连边,流量为inf,费用为输入的费用;商店与超级汇点连边,流量为商店第 k 个商品的需求量,费用为0。

别忘了判断能否完全供应

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1005;
const int M = 2e4 + 10;

struct Edge {
    int to, next, cap, flow, cost;
}edge[M];
int head[N], tot, pre[N], dis[N], n, m, s, t, minCost, maxFlow;
bool vis[N];
int cost[105][105][105], in[105][105], out[105][105], u[105], v[105];

void init() {
    tot = 0;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v, int cap, int cost) { //cap流量 cost花费
    edge[tot].to = v;
    edge[tot].cap = cap;
    edge[tot].cost = cost;
    edge[tot].flow = 0;
    edge[tot].next = head[u];
    head[u] = tot++;

    edge[tot].to = u;
    edge[tot].cap = 0;
    edge[tot].cost = -cost;
    edge[tot].flow = 0;
    edge[tot].next = head[v];
    head[v] = tot++;
}

bool spfa(int s, int t) {
    queue<int>q;
    for(int i = 0; i < N; ++i) {
        dis[i] = inf;
        vis[i] = 0;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = 1;
    q.push(s);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        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] = 1;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t] == -1) return 0;
    else return 1;
}

int MCMF(int s, int t) {
    minCost = 0;
    maxFlow = 0;
    while(spfa(s, t)) {
        int minn = inf;
        for(int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) {
            if(minn > edge[i].cap - edge[i].flow)
                minn = edge[i].cap - edge[i].flow;
        }
        for(int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) {
            edge[i].flow += minn;
            edge[i ^ 1].flow -= minn;
            minCost += edge[i].cost * minn;
        }
        maxFlow += minn;
    }
    return maxFlow;
}

int main() {
    int a, k;
    while(~scanf("%d%d%d", &n, &m, &k) && n && m && k) {
        s = 0;
        t = n + m + 1;
        for(int i = 1; i <= k; ++i) u[i] = 0;
        for(int i = 1; i <= k; ++i) v[i] = 0;
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= k; ++j) {
                scanf("%d", &out[j][i]);
                v[j] += out[j][i];
            }
        }
        for(int i = 1; i <= m; ++i) {
            for(int j = 1; j <= k; ++j) {
                scanf("%d", &in[j][i]);
                u[j] += in[j][i];
            }
        }
        for(int q = 1; q <= k; ++q)
            for(int i = 1; i <= n; ++i)
                for(int j = 1; j <= m; ++j)
                    scanf("%d", &cost[q][i][j]);
        bool flag = 1;
        for(int i = 1; i <= k; ++i) {
            if(u[i] < v[i]) {
                flag = 0;
                break;
            }
        }
        if(!flag) {
            printf("-1\n");
            continue;
        }
        int ans = 0;
        for(int q = 1; q <= k; ++q) {
            init();
            memset(edge, 0, sizeof(edge));
            for(int i = 1; i <= m; ++i)
                addedge(s, i, in[q][i], 0);
            for(int i = 1; i <= n; ++i)
                addedge(i + m, t, out[q][i], 0);
            for(int i = 1; i <= n; ++i)
                for(int j = 1; j <= m; ++j)
                    addedge(j, i + m, inf, cost[q][i][j]);
            MCMF(s, t);
            ans += minCost;
        }
        printf("%d\n", ans);
    }
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值