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. cookie、session和中间件

    目录 cookie和session cookie与session原理 cookie Google浏览器查看cookie Django操作cookie 获取cookie 设置cookie 删除cooki ...

  2. React Native 中不用手点击,代码实现切换底部tabBar

    参考:https://www.jianshu.com/p/02c630ed7725 tabBar1页面有按钮,点击切换到tabBar2 直接用this.props.navigation.navigat ...

  3. Vue框架基础概要

    Vue.js是什么? Vue.js(读音 /vjuː/,类似于 view 的读音)是一套构建用户界面(user interface)的渐进式框架.与其他重量级框架不同的是,Vue 从根本上采用最小成本 ...

  4. 跳转控制语句return

    return语句的作用不是为了跳出循环,更常用的功能是结束一个方法,也就是退出一个方法,跳转到上层调用的方法处. 演示案例: 结束循环其实是结束了main方法 public static void m ...

  5. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  6. Finer Resolution Observation and Monitoring -Global Land Cover更精细的分辨率观测和监测-全球土地覆盖

    http://data.ess.tsinghua.edu.cn/ 全球土地覆盖数据是了解人类活动与全球变化之间复杂互动的关键信息来源.FROM-GLC(全球土地覆盖的精细分辨率观测和监测)是首个使用陆 ...

  7. MapServer教程3

    Compiling on Unix Compiling on Win32 PHP MapScript Installation .NET MapScript Compilation IIS Setup ...

  8. SpringBoot学习-第一章

    1.SpringBoot入门 开发环境:JDK1.8 开发工具:IDEA2017.3.1 1.简介: Spring Boot让我们的Spring应用变的更轻量化.比如:你可以仅仅依靠一个Java类来运 ...

  9. [CSP-S模拟测试]:简单的序列(DP)

    题目描述 从前有个括号序列$s$,满足$|s|=m$.你需要统计括号序列对$(p,q)$的数量. 其中$(p,q)$满足$|p|+|s|+|q|=n$,且$p+s+q$是一个合法的括号序列. 输入格式 ...

  10. 转载:String.format()的详细用法

    转载自:https://blog.csdn.net/anita9999/article/details/82346552 问题 在开发的时候一段字符串的中间某一部分是需要可变的 比如一个Textvie ...