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 题意:有n个商店,m个提供商,k种商品
接下来 n*k的矩阵,表示每个商店需要每个商品的数目;
再接下来m*k矩阵,表示每个提供商拥有每个商品的个数。
然后,对于每个物品k,都有n*m的矩阵。
i行j列表示:
从j提供商向i商店运送一个k商品的代价是多少。
判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用。 思路:关键是建图,建立一个源点是s = 0 和 汇点 t = n+m+1;
源点到m个供应商,费用为0,容量是这个提供商能够提供这种物品的数量;
每个供应商到每个商店,费用为输入的费用(添加双向边),容量为无穷大;

每个商店到汇点,费用为0,容量为这个商店需要这种商品的数目。 还要考虑到供不应求的情况,当需求量大于供应量时,不能满足,输出-1.
对于第r种商品,若它的需求量大于最大流量,也不能满足,输出-1;
对每1个商品进行建图寻找增光路,最后累加输出最小费用就行了;
 #include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f;
int n,m,k;
int nn[maxn][maxn],need[maxn];
int hh[maxn][maxn],have[maxn];
int cost[maxn][maxn],res[maxn][maxn];//cost[i][j]表示i到j的费用,res[i][j]表示i到j的当前容量
int s, t;
int mincost,maxflow;
int dis[maxn],pre[maxn];
void build_graph(int x)
{
memset(res,,sizeof(res));
for(int i = ; i <= m; i++)
res[s][i+n] = hh[i][x];//源点指向每个供应商,费用为0,容量为该供应商提供的第r种商品
for(int i = ; i <= n; i++)
res[i][t] = nn[i][x];//所有商店指向汇点,费用为0,容量为该供应商需要的第r种商品
for(int i = ; i <= m; i++)
{
for(int j = ; j <= n; j++)
res[i+n][j] = INF;//每个供应商指向每个商店,容量为无穷大。
}
}
void spfa()
{
queue<int>que;
while(!que.empty())
que.pop();
memset(pre,-,sizeof(pre));
int inque[maxn];
memset(inque,,sizeof(inque));
for(int i = s; i <= t; i++)
dis[i] = INF;
dis[s] = ;
inque[s] = ;
que.push(s); while(!que.empty())
{
int u = que.front();
que.pop();
inque[u] = ; for(int i = ; i <= n+m+; i++)
{
if(res[u][i] && dis[i] > dis[u] + cost[u][i])
{
dis[i] = dis[u] + cost[u][i];
pre[i] = u;
if(!inque[i])
{
inque[i] = ;
que.push(i);
}
}
}
}
} void MCMF()
{
maxflow = ;//增光第r种商品的总流量,初始化为0;
int minflow;//当前增光路上可增加的最小流量;
while()
{
spfa();
if(pre[t] == -)//找不到增光路,退出
break; minflow = INF;
for(int u = t; u != s; u = pre[u])
{
minflow = min(minflow,res[ pre[u] ][u]);//寻找该增光路上的最小流量
}
for(int u = t; u != s; u = pre[u])
{
res[ pre[u] ][u] -= minflow;
res[u][ pre[u] ] += minflow;
}
maxflow += minflow;
mincost += minflow*dis[t];
}
} int main()
{
while(~scanf("%d %d %d",&n,&m,&k))
{
if(n == && m == && k == )
break;
bool flag = ;
s = ;//源点
t = n+m+;//汇点
mincost = ;//最小费用初始化;
memset(need,,sizeof(need));
memset(have,,sizeof(have));
//nn[i][j]表示第i个商店需求nn[i][j]个第j种商品;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= k; j++)
{
scanf("%d",&nn[i][j]);
need[j] += nn[i][j];
}
}
//hh[i][j]表示第i个供应商拥有hh[i][j]个第j中商品;
for(int i = ; i <= m; i++)
{
for(int j = ; j <= k; j++)
{
scanf("%d",&hh[i][j]);
have[j] += hh[i][j];
}
}
//如果第i种商品的需求量大于供应量,标记为1,但后面的仍然要继续输入
for(int i = ; i <= k; i++)
{
if(need[i] > have[i])
{
flag = ;
break;
}
}
//下面输入k个n*m的矩阵,其第i行第j列表示第j个供应商向第i个商店运送第k个商品的单位费用;
for(int r = ; r <= k; r++)
{
memset(cost,,sizeof(cost));
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
scanf("%d",&cost[j+n][i]);
cost[i][j+n] = -cost[j+n][i];//注意添加双向边
}
}
if(flag) continue;//如果已经不合法,就不用建图,但数据要继续输入 build_graph(r);
MCMF();
if(need[r] > maxflow) flag = ;//如果第r种商品的需求量大于最大流量,也不合法。
}
if(flag) printf("-1\n");
else printf("%d\n",mincost);
}
return ;
}
												

Minimum Cost(最小费用最大流)的更多相关文章

  1. POJ2516:Minimum Cost(最小费用最大流)

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19088   Accepted: 6740 题目链 ...

  2. POJ2516 Minimum Cost —— 最小费用最大流

    题目链接:https://vjudge.net/problem/POJ-2516 Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Tota ...

  3. POJ 2516 Minimum Cost [最小费用最大流]

    题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...

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

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

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

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

  6. POJ2516 Minimum Cost(最小费用最大流)

    一开始我把每个店主都拆成k个点,然后建图..然后TLE.. 看题解= =哦,愚钝了,k个商品是独立的,可以分别跑k次最小费用最大流,结果就是k次总和.. #include<cstdio> ...

  7. POJ2516 Minimum Cost【最小费用最大流】

    题意: 有N个客户,M个仓库,和K种货物.已知每个客户需要每种货物的数量,每个仓库存储每种货物的数量,每个仓库运输各种货物去各个客户的单位费用.判断所有的仓库能否满足所有客户的需求,如果可以,求出最少 ...

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

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

  9. POJ--2516--Minimum Cost【最小费用最大流】

    链接:http://poj.org/problem?id=2516 题意:有k种货物,n个客户对每种货物有一定需求量,有m个仓库.每一个仓库里有一定数量的k种货物.然后k个n*m的矩阵,告诉从各个仓库 ...

随机推荐

  1. Unity 3D 文件导入出错误解决方法以及unity圣典离线版下载地址

    1.安装unity 时我选择了free版的,打开已有项目时出现如下错误提示. 解决方法:先把要导入的文件先拷贝到unity3d安装目录下对应的文件夹内,之后再返回unity3d软件,右键选择“导入”. ...

  2. cognos 10.2.2 Framework manager使用”数据源”新建查询主题

    又做了一个简单的报表,就是在Framework Manager中写个sum()的sql出个报表,可以使用使用"数据源"新建查询主题 配置查询主题后修改SQL,注意全部都是大写,要和 ...

  3. javascript调用oc的方法

    1.引入#import <JavaScriptCore/JavaScriptCore.h> 2.JSContext *jsContext = [self.webView valueForK ...

  4. Objective-C 获取当前执行函数的名称

    当打印日志时为了方便跟踪,需要抛出当前执行函数的名称,一样可以使用c++的宏__FUNCTION__实现. @implementation CTPerson -(void)show { NSLog(@ ...

  5. 自己在使用的English词典

    一.ESL/非母语词典 二.EFL/母语词典 1.American Heritage Dictionary 2.World Book Dictionary 3.Oxford Dictionary of ...

  6. 九度OJ 1107 搬水果 -- 哈夫曼树 2011年吉林大学计算机研究生机试真题

    题目地址:http://ac.jobdu.com/problem.php?pid=1107 题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果 ...

  7. ASP.NET中的验证控件

    ASP.NET提供了如下的控件: RequiredFieldValidator: 字段必填 (ControlTovalidate设定要验证的控件) RangeValidator: 值在给定的最大值,最 ...

  8. js清空前后空格

    function trim(sValue){                var lastValue=this.replace(/(^\s*)|(\s*$)/g,"");     ...

  9. 【原创】Linux下获取命令的帮助与常用命令

    Linux中的shell命令一般是执行步骤:用户在终端输入命令回车,系统内核会在当前用户的环境变量PATH中去读取环境变量的值 变量的值就是命令的路径,命令路径不只一个,于是系统会从这些路径中从左至右 ...

  10. C#基础(二)——C#中的构造函数

    构造函数主要是用来创建对象时为对象赋初值来初始化对象.总与new运算符一起使用在创建对象的语句中 .A a=new A(); 构造函数具有和类一样的名称:但它是一个函数具有函数的所有特性,同一个类里面 ...