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

这个直接把每件物品单出来考虑完事,单独建图跑就行,然后就很简单了。就变成了普通的费用流问题,那么建图套模板即可!

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
#define INF 1e9
using namespace std;
const int maxn=200+10; struct Edge
{
int from,to,cap,flow,cost;
Edge(){}
Edge(int f,int t,int c,int fl,int co):from(f),to(t),cap(c),flow(fl),cost(co){}
}; struct MCMF
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn]; void init(int n,int s,int t)
{
this->n=n, this->s=s, this->t=t;
edges.clear();
for(int i=0;i<n;++i) G[i].clear();
} void AddEdge(int from,int to,int cap,int cost)
{
edges.push_back(Edge(from,to,cap,0,cost));
edges.push_back(Edge(to,from,0,0,-cost));
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
} bool BellmanFord(int &flow, int &cost)
{
for(int i=0;i<n;++i) d[i]=INF;
memset(inq,0,sizeof(inq));
d[s]=0, a[s]=INF, inq[s]=true, p[s]=0;
queue<int> Q;
Q.push(s);
while(!Q.empty())
{
int u=Q.front(); Q.pop();
inq[u]=false;
for(int i=0;i<G[u].size();++i)
{
Edge &e=edges[G[u][i]];
if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
{
d[e.to]= d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]= min(a[u],e.cap-e.flow);
if(!inq[e.to]){ Q.push(e.to); inq[e.to]=true; }
}
}
}
if(d[t]==INF) return false;
flow +=a[t];
cost +=a[t]*d[t];
int u=t;
while(u!=s)
{
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -=a[t];
u = edges[p[u]].from;
}
return true;
}
int Min_cost()
{
int flow=0,cost=0;
while(BellmanFord(flow,cost));
return cost;
}
}MM; int n,m,k;
int need[50+5][50+5]; //need[i][j]表i顾客对j商品的需求量
int have[50+5][50+5]; //have[i][j]表i仓库对j商品的提供量
int cost[50+5][50+5][50+5]; //cost[x][i][j] 表j仓库到i顾客对x商品的单位运费 int main()
{
while(scanf("%d%d%d",&n,&m,&k)==3 && n)
{
int goods[maxn];//货物需求量,用来判断货物是否足够
int enough=true;//初始货物充足
memset(goods,0,sizeof(goods)); for(int i=1;i<=n;++i)
for(int j=1;j<=k;++j)
{
scanf("%d",&need[i][j]);
goods[j]+= need[i][j];
} for(int i=1;i<=m;++i)
for(int j=1;j<=k;++j)
{
scanf("%d",&have[i][j]);
goods[j] -=have[i][j];
} for(int h=1;h<=k;++h)
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
scanf("%d",&cost[h][i][j]); for(int i=1;i<=k;++i)if(goods[i]>0)//货物不足,不用计算了
{
enough=false;
break;
}
if(!enough)//初始货物不足
{
printf("-1\n");
continue;
} int min_cost=0;
for(int g=1;g<=k;++g)
{
int src=0, dst=n+m+1;
MM.init(n+m+2,src,dst);
for(int i=1;i<=m;++i) MM.AddEdge(src,i,have[i][g],0);
for(int i=1;i<=n;++i) MM.AddEdge(m+i,dst,need[i][g],0);
for(int i=1;i<=m;++i)
for(int j=1;j<=n;++j)
{
MM.AddEdge(i,j+m,INF,cost[g][j][i]);
}
min_cost += MM.Min_cost();
}
printf("%d\n",min_cost);
}
return 0;
}

图论--网络流--费用流--POJ 2156 Minimum Cost的更多相关文章

  1. 图论--网络流--费用流POJ 2195 Going Home

    Description On a grid map there are n little men and n houses. In each unit time, every little man c ...

  2. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

  3. 图论--网络流--最大流--POJ 1698 Alice's Chance

    Description Alice, a charming girl, have been dreaming of being a movie star for long. Her chances w ...

  4. 图论--网络流--最大流 POJ 2289 Jamie's Contact Groups (二分+限流建图)

    Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very ...

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

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

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

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

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

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

  8. 图论-zkw费用流

    图论-zkw费用流 模板 这是一个求最小费用最大流的算法,因为发明者是神仙zkw,所以叫zkw费用流(就是zkw线段树那个zkw).有些时候比EK快,有些时候慢一些,没有比普通费用流算法更难,所以学z ...

  9. POJ 2516 Minimum Cost (费用流)

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

随机推荐

  1. 2017蓝桥杯承压计算(C++ B组)

    标题:承压计算X星球的高科技实验室中整齐地堆放着某批珍贵金属原料.每块金属原料的外形.尺寸完全一致,但重量不同.金属材料被严格地堆放成金字塔形.                            ...

  2. 插入排序(C语言版)

    #include<iostream>using namespace std;int n;void lan(int a[],int size){ for(int i = 0;i < s ...

  3. 抓包——HTTP分析

      1.什么是HTTP请求(底层使用scoket TCP技术) HTTP是超文本传输协议.底层使用的scoket tcp长连接.基于请求和响应  同步请求. 2.重定向底层: 重定向原理:为什么会产生 ...

  4. 多数据源系统接入mybatis-plus, 实现动态数据源、动态事务。

    目录: 实现思想 导入依赖.配置说明 代码实现 问题总结 一.实现思想 接手一个旧系统,SpringBoot 使用的是纯粹的 mybatis ,既没有使用规范的代码生成器,也没有使用 JPA 或者 m ...

  5. MODIS系列之NDVI(MOD13Q1)一:数据下载(二)基于FTP

    这一篇我们来介绍下MODIS数据的下载方式.当然这边主要是介绍国外网站的下载方式,国内网站的普遍是在地理空间数据云和遥感集市下载.国外网站(NASA官网)下载方式主要介绍两种.本篇主要针对第一种方式, ...

  6. Python操作rabbitmq系列(四):根据类型订阅消息

    在上一章中,所有的接收端获取的所有的消息.这一章,我们将讨论,一些消息,仍然发送给所有接收端.其中,某个接收端,只对其中某些消息感兴趣,它只想接收这一部分消息.如下图:C1,只对error感兴趣,C2 ...

  7. AJ学IOS(16)UI之XIB自定义Cell实现团购UI

    AJ分享,必须精品 先看效果图 自定义Cell 本次主要是自定义Cell的学习 实现自定义Cell主要有三种方法:按照使用的频繁度排序: XIB > 纯代码 > StoryBoard XI ...

  8. 【MyBatis深入剖析】应用分析与最佳实践(下)

    MyBatis编程式开发 MyBatis编程式开发步骤 MyBatis和MySQL Jar包依赖 全局配置文件mybatis-config.xml 映射器Mapper.xml Mapper接口 编程式 ...

  9. go获取当前项目下所有依赖包

    在设置好GOPATH,GOROOT的环境变量的情况下. 在项目配置好pkg.bin.src等这几个目录的情况,进入src目录. 在终端,输入:go get ./... 即可获得所有依赖包.

  10. Python玩转人工智能最火框架 TensorFlow应用实践 学习 教程

    随着 TensorFlow 在研究及产品中的应用日益广泛,很多开发者及研究者都希望能深入学习这一深度学习框架.而在昨天机器之心发起的框架投票中,2144 位参与者中有 1441 位都在使用 Tenso ...