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. .gitignore配置文件

    .gitignore文件 在使用Git的过程中,我们希望有些文件比如日志.临时文件.编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交.git提供一个.gitigno ...

  2. 【LuoguP3270】[JLOI2016] 成绩比较

    题目链接 题目描述 G系共有n位同学,M门必修课.这N位同学的编号为0到N-1的整数,其中B神的编号为0号.这M门必修课编号为0到M-1的整数.一位同学在必修课上可以获得的分数是1到Ui中的一个整数. ...

  3. 接口测试的一些FAQ

    近期在学习接口测试,总会有很多问题,故把前任给的经验做个记录下. 1,测试一条delete接口,delete请求后要检验什么内容: 原来删除有分为:物理删除,删除资源,删除关系,软删除 一般delet ...

  4. springboot自定义异常视图

    一.源码分析 先看源码再写自己的自定义异常视图         resolveErrorView()函数首先调用了一个返回ModelAndView的函数,该函数所需的参数是一个状态码的字符串,和一个m ...

  5. 6392. 【NOIP2019模拟2019.10.26】僵尸

    题目描述 题解 吼题但题解怎么这么迷 考虑一种和题解不同的做法(理解) 先把僵尸离散化,h相同的钦(ying)点一个大小 (可以发现这样每种情况只会被算正好一次) 计算完全被占领的方案,然后1-方案/ ...

  6. adb打开系统设置的命令

    adb命令打开手机设置页面 设置主页面adb shell am start com.android.settings/com.android.settings.Settings 安全adb shell ...

  7. luogu P1037 产生数 x

    P1037 产生数 题目描述 给出一个整数 n(n<10^30) 和 k 个变换规则(k<=15). 规则: 一位数可变换成另一个一位数: 规则的右部不能为零. 例如:n=234.有规则( ...

  8. HDU-6704 K-th occurrence

    Description You are given a string S consisting of only lowercase english letters and some queries. ...

  9. HDU 4348 SPOJ 11470 To the moon

    Vjudge题面 Time limit 2000 ms Memory limit 65536 kB OS Windows Source 2012 Multi-University Training C ...

  10. Laya 爆改Laya IDE和Laya引擎使其支持2D粒子爆发模式

    Laya 爆改Laya IDE和Laya引擎使其支持2D粒子爆发模式 @author ixenos 2019-11-01 19:47:26 1. 修改IDE的功能需要深入到/resources/app ...