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. 创建kudu数据集测试总结

    参考文档: https://cloud.tencent.com/developer/article/1474797 https://www.tgshenghe.com/a77nr1/nzt9t1.ht ...

  2. Docker(三):Docker入门教程-CentOS Docker 安装

    CentOS Docker 安装 Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 前提条件 目前,CentOS 仅发 ...

  3. [洛谷P3243] 菜肴制作

    问题描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予1到N的顺序编号,预估质量最高的菜肴编号为1. 由于菜肴 ...

  4. Oracle RAC数据泵导出问题处理

    1. 设置导出文件路径 sqlplus / as sysdba SQL> alter session set container=spdb1pdb; SQL> create directo ...

  5. vue addRoutes路由动态加载

    需求:增加权限控制,实现不同角色显示不同的路由导航 思路:每次登陆后请求接口返回当前角色路由 核心方法:vue-router2.2.0的addRoutes方法 + vuex 以下是我实现的获取菜单路由 ...

  6. B/S超大文件上传与下载

    最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...

  7. Codeforces Round #603 (Div. 2)F. Economic Difficulties

    F. Economic Difficulties 题目链接: https://codeforces.com/contest/1263/problem/F 题目大意: 两棵树,都有n个叶子节点,一棵树正 ...

  8. Codeforce |Educational Codeforces Round 77 (Rated for Div. 2) B. Obtain Two Zeroes

    B. Obtain Two Zeroes time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. 180128-----Java面试题

    1 不用第三个变量,交换两个的值 a=1;b=2;a=a+b;b=a-b;a=a-b; 2 Java动态代理用什么实现? 反射

  10. WEB Fuzz中需要关注的7种响应

    WEB应用模糊测试(WEB Fuzz)是一种特殊形式的网络协议模糊测试,专门关注遵循HTTP规范的网络数据包. WEB Fuzz并不是新的概念,目前有多种WEB应用模糊测试器(WEB Fuzzer), ...