Minimum Cost

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 19883   Accepted: 7055

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

Source

都在代码里了,不不建议抄袭代码,代码里有些调试代码,有需要的可以看代码之前注释,前面是解释,精髓在最后三行。

 /*
本题心得:一开始做题就有种感觉需要对商品拆点,然后满足每个商人,但是这样的话每个商品要拆为n个点,必然会有很大的空间浪费造成tle,
实在没思路之后看了博客,看到说每种商品都是独立的,意思就是把商人需要的每种物品都单独买,然后统计最后结果就行了,这里有一个细节就是,
因为目的是满足所有商人情况下的最小费用,那也就是最小费用最大流,所以我们事先判断某种商品是否够用,如果够用,那么最大流一定是满载的,
所以不必担心找不到最大流,就找最小花费就行了。
对于每个商品,我们记得要清空head数组,额贼,这个把我坑了好久,后来想如果不清空必然会在spfa中造成无限循环(想想为什么?),所以对每件
商品都需要init,对于每件商品,我们建立超级源点指向那些供应商,容量为最大供应数目花费为0,对于每个商人,我们建立一条边指向超级汇点,容量为商人
对这件商品的需求数目(限制每个商人得到的物品数),花费为0,对于每个供应商和他的商人之间建立一条由供应商指向商人的边,cap为inf(由于前面我们已经限制了每个供应商可以提供的物品)
花费为这个供应商对这个商人供应这件物品的cost,跑一波费用流就ojk了。
这样我们就
通过供应商 -> 商人 限制了价格
通过 s -> 供应商 限制了供应个数
通过商人 -> t 限制了商人的需求数目。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int maxn = + , maxm = 1e4 + , inf = 0x3f3f3f3f;
int want[maxn][maxn], supply[maxn][maxn], sumwant[maxn], sumsupply[maxn], costij[maxn][maxn][maxn];
struct Edge {
int to, next, cap, flow, cost, from;
} edge[maxm];
int head[maxn << ], tot;
int pre[maxn << ], dis[maxn << ];
bool vis[maxn << ]; int N; void init(int n) {
N = n;
tot = ;
memset(head, -, sizeof head);
} void addedge(int u, int v, int cap, int cost) {
edge[tot].to = v; edge[tot].cap = cap; edge[tot].cost = cost; edge[tot].flow = ; edge[tot].from = u;
edge[tot].next = head[u]; head[u] = tot ++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].cost = -cost; edge[tot].flow = ; edge[tot].from = v;
edge[tot].next = head[v]; head[v] = tot ++;
} bool spfa(int s, int t) {
queue <int> que;
// memset(dis, inf, sizeof dis);
// memset(vis, false, sizeof vis);
// memset(pre, -1, sizeof pre);
for(int i = ; i <= N; i ++) {
dis[i] = inf;
vis[i] = false;
pre[i] = -;
}
dis[s] = ;
vis[s] = true;
que.push(s);
while(!que.empty()) {
// printf("in bfs");
int u = que.front();
que.pop();
vis[u] = false;
for(int i = head[u]; ~i; 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] = true;
// printf("now in push of bfs");
que.push(v);
}
}
}
}
return ~pre[t];
// if(pre[t] == -1) return false;
// else return true;
} int mincostmaxflow(int s, int t) {
int cost = ;
while(spfa(s, t)) {
// printf("spfa is true");
int Min = inf;
for(int i = pre[t]; ~i; i = pre[edge[i ^ ].to]) {
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
// printf("now is find min");
}
for(int i = pre[t]; ~i; i = pre[edge[i ^ ].to]) {
edge[i].flow += Min;
edge[i ^ ].flow -= Min;
cost += edge[i].cost * Min;
// printf("now is update");
}
}
return cost;
} int main() {
int n, m, k;
while(~scanf("%d %d %d", &n, &m, &k) && (n | m | k)) { memset(want, , sizeof want);
memset(supply, , sizeof supply);
memset(sumwant, , sizeof sumwant);
memset(sumsupply, , sizeof sumsupply);
for(int i = ; i <= n; i ++) {
for(int j = ; j <= k; j ++) {
scanf("%d", &want[i][j]);
sumwant[j] += want[i][j];
}
}
for(int i = ; i <= m; i ++) {
for(int j = ; j <= k; j ++) {
scanf("%d", &supply[i][j]);
sumsupply[j] += supply[i][j];
}
}
bool flag = true;
for(int i = ; i <= k; i ++) {
if(sumwant[i] > sumsupply[i]) {
flag = false;
break;
}
}
for(int q = ; q <= k; q ++) {
for(int i = ; i <= n; i ++) {
for(int j = ; j <= m; j ++) {
scanf("%d", &costij[q][i][j]);//第q件物品,第i个人从第j个供应商的花费
}
}
}
int s = , t = m + n + , mcmf = ;
if(flag) {
for(int q = ; q <= k; q ++) {
init(n + m + );
// printf("***************\n");
for(int i = ; i <= m; i ++) {
addedge(s, i, supply[i][q], );
}
for(int i = ; i <= n; i ++) {
addedge(i + m, t, want[i][q], );
}
for(int i = ; i <= n; i ++) {
for(int j = ; j <= m; j ++) {
addedge(j, i + m, inf, costij[q][i][j]);
}
}
// for(int i = 0; i < tot; i ++) {
// printf("%d -> %d\n", edge[i].from, edge[i].to);
// }
mcmf += mincostmaxflow(s, t);
}
printf("%d\n", mcmf);
} else printf("-1\n");
}
return ;
}

poj-2516.minimum cost(k次费用流)的更多相关文章

  1. POJ 2516 Minimum Cost (最小费用最大流)

    POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...

  2. Poj 2516 Minimum Cost (最小花费最大流)

    题目链接: Poj  2516  Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...

  3. POJ - 2516 Minimum Cost(最小费用最大流)

    1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...

  4. POJ 2516 Minimum Cost (网络流,最小费用流)

    POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...

  5. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  6. POJ - 2516 Minimum Cost 每次要跑K次费用流

    传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...

  7. POJ 2516 Minimum Cost(最小费用流)

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  8. POJ 2516 Minimum Cost 最小费用流

    题目: 给出n*kk的矩阵,格子a[i][k]表示第i个客户需要第k种货物a[i][k]单位. 给出m*kk的矩阵,格子b[j][k]表示第j个供应商可以提供第k种货物b[j][k]单位. 再给出k个 ...

  9. POJ 2516 Minimum Cost

    每个物品分开做最小费用最大流. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...

随机推荐

  1. GUI学习之十二——QTextEdit学习总结

    在学习了QFrame和QAbstractScrollArea两个父类后,接下来是一个重头戏:QTextEdit. 一.QTextEdit特性 QTextEdit是一个高级的WYSIWYG(What Y ...

  2. YNOI2016:掉进兔子洞 (莫队+bitset)

    YNOI2016:掉进兔子洞 题意简述: 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立. 注意这里删掉指的是一个一个删,不是把等于这 ...

  3. vue组件结构

    1.组件结构 2.项目结构

  4. M(model)V(view)C(controller,serlvet),(分) 静态工厂模式,单例模式

  5. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  6. oracle各服务说明及cmd启动启动命令

    成功安装Oracle 11g后,共有7个服务,一.这七个服务的含义分别为:1. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写入服务,VSS(Volume Sh ...

  7. PHP 设计模式总结

    回想了一下php的设计模式,好像记得不完全了.此处对php设计模式重新做一下复习总结. 单例模式 单例模式的核心只包括一个特殊的类,保证系统中只能有一个实例,即一个类中只能有一个实例化对象,避免系统中 ...

  8. 安装fedora23后的一些杂项设置

    Boxes是创建虚拟机的技术 tweak: 拧, 捏; 微调 he gave the boy's ear a painful tweak. it's a small tweak over the ra ...

  9. iOS 命令行打包--xcworkspace

    参考: 打包的具体操作步骤: https://www.jianshu.com/p/6a0aa8cd2e97 打包时使用到的参数详解,参考这篇: https://debugtalk.com/post/i ...

  10. PLSQL设置数据库选项

    1.将数据库安装目录下的"NETWORK"文件夹复制到client(客户端)安装目录下 : 2.修改"NETWORK"-->"ADMIN&quo ...