Minimum Cost
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 13511   Accepted: 4628

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. 1 3 3
  2. 1 1 1
  3. 0 1 1
  4. 1 2 2
  5. 1 0 1
  6. 1 2 3
  7. 1 1 1
  8. 2 1 1
  9.  
  10. 1 1 1
  11. 3
  12. 2
  13. 20
  14.  
  15. 0 0 0

Sample Output

  1. 4
  2. -1

这一题的边有两个性质,

1 容量:供给-种类/需求-种类

2 价格: 供给-种类-需求

怎么想都想不到怎么保留这两种性质建边

看到小you的博客,可以分种类建图,恍然大悟

于是对每个种类分成两种边

1 源点->供给 需求->最终汇点 用于控制流量,价格为0

2 供给->需求 用于控制价格,流量为inf,

值得一提的是供给->需求是单向边,返回的边价格应该是负数,在这里卡了一次,还有每次流量应该是需求量

  1. #include <cstdio>
  2. #include <vector>
  3. #include <queue>
  4. #include <cstring>
  5. using namespace std;
  6. const int maxn=;
  7. const int maxm=;
  8. const int maxk=;
  9. const int maxnum=;
  10. const int inf =0x7fffffff;
  11.  
  12. int f[maxnum][maxnum];//s 151 t 152
  13. int cons[maxk][maxnum];
  14. int cost[maxk][maxnum][maxnum];
  15. int e[maxnum][maxnum];
  16. int len[maxnum];
  17.  
  18. const int sups=,supt=;
  19. int sum,n,m,k;
  20.  
  21. int s1[maxk],s2[maxk];//s1 0-n-1 s2 n-n+m
  22. bool input(){
  23. memset(cons,,sizeof(cons));
  24. memset(s1,,sizeof(s1));
  25. memset(s2,,sizeof(s2));
  26.  
  27. sum=;
  28. if(scanf("%d%d%d",&n,&m,&k)!=)return false;
  29. if(n==&&m==&&k==)return false;
  30. for(int i=;i<n;i++){
  31. int c;
  32. for(int j=;j<k;j++){
  33. scanf("%d",&c);
  34. cons[j][i]=c;
  35. s1[j]+=c;
  36. }
  37. }
  38. for(int i=n;i<n+m;i++){
  39. int c;
  40. for(int j=;j<k;j++){
  41. scanf("%d",&c);
  42. cons[j][i]=c;
  43. s2[j]+=c;
  44. }
  45. }
  46.  
  47. for(int i=;i<k;i++){
  48. for(int j=;j<n;j++){
  49. for(int ii=n;ii<n+m;ii++){
  50. int c;
  51. scanf("%d",&c);
  52. cost[i][j][ii]=c;cost[i][ii][j]=-c;
  53. }
  54. }
  55. }
  56.  
  57. for(int i=;i<n;i++){
  58. for(int j=n;j<n+m;j++){
  59. e[i][j-n]=j;
  60. e[j][i]=i;
  61. e[i][m]=;
  62. e[j][n]=;
  63. e[][i]=i;
  64. e[][j-n]=j;
  65. }
  66. }
  67. fill(len,len+n,m+);
  68. fill(len+n,len+n+m,n+);
  69. len[]=n;
  70. len[]=m;
  71. return true;
  72. }
  73. void build(int kind){
  74. memset(f,,sizeof(f));
  75. for(int i=;i<n;i++)f[][i]=cons[kind][i];
  76. for(int i=n;i<n+m;i++)f[i][]=cons[kind][i];
  77. for(int i=;i<n;i++){
  78. for(int j=n;j<n+m;j++){
  79. f[i][j]=inf;
  80. }
  81. }
  82. }
  83. int d[maxnum],pre[maxnum];
  84. bool vis[maxnum];
  85. queue<int >que;
  86. int mincostmaxflow(int s,int flow,int kind){
  87. build(kind);
  88. int res=;
  89. while(flow>){
  90. fill(d,d+,inf);
  91. memset(vis,,sizeof(vis));
  92. d[s]=;
  93. que.push(s);
  94. while(!que.empty()){
  95. int fr=que.front();que.pop();
  96. vis[fr]=false;
  97. for(int i=;i<len[fr];i++){
  98. int t=e[fr][i];
  99. if(f[fr][t]>&&d[t]>d[fr]+cost[kind][fr][t]){
  100. d[t]=d[fr]+cost[kind][fr][t];
  101. pre[t]=fr;
  102. if(!vis[t]){
  103. que.push(t);
  104. vis[t]=true;
  105. }
  106. }
  107. }
  108. }
  109. if(d[supt]==inf)return -;
  110. int sub=flow;
  111. for(int v=supt;v!=sups;v=pre[v]){
  112. sub=min(sub,f[pre[v]][v]);
  113. }
  114. flow-=sub;
  115. res+=sub*d[supt];
  116. for(int v=supt;v!=sups;v=pre[v]){
  117. f[v][pre[v]]+=sub;
  118. f[pre[v]][v]-=sub;
  119. }
  120. }
  121. return res;
  122. }
  123. int main(){
  124. while(input()){
  125. int ans=;
  126. bool sign=false;
  127. for(int i=;i<k;i++){
  128. if(s1[i]>s2[i]){printf("-1\n");sign=true;break;}
  129. }
  130. if(sign)continue;
  131. for(int i=;i<k;i++){
  132. int res=mincostmaxflow(,s1[i],i);
  133. if(res==-){printf("-1\n");sign=true;break;}
  134. ans+=res;
  135. }
  136. if(sign)continue;
  137. printf("%d\n",ans);
  138.  
  139. }
  140. return ;
  141. }

POJ 2516 Minimum Cost 最小费用流 难度:1的更多相关文章

  1. 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个 ...

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

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

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

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

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

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

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

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  6. POJ 2516 Minimum Cost (费用流)

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

  7. POJ - 2516 Minimum Cost 每次要跑K次费用流

    传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...

  8. POJ 2516 Minimum Cost(拆点+KM完备匹配)

    题目链接:http://poj.org/problem?id=2516 题目大意: 第一行是N,M,K 接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况 接下来M行:第j行有K个数字表示 ...

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

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

随机推荐

  1. VirtualBox 安装XP虚拟机需要注意的问题

    1.首先要有xp系统的镜像文件 http://pan.baidu.com/s/1i4xrwdJ 2.新建虚拟机,并安装 http://www.pczhishi.cn/shipin/107.html 3 ...

  2. 关于fragment点击穿透的问题

    当一个activity有多个fragment的时候,点击当前显示的fragment,如果点击处在其他fragment中也有相应的控件,那么可能会点击穿透,有响应另外fragment事件的趋势.但是这个 ...

  3. SPOJ Hacking(字典树 + 搜索)题解

    思路1:字典树存每个串,然后dfs遍历是否存在.这里有个技巧,如果每次都重新初始化字典树为-1,那么会超时,所以我先初始化为-1,然后设一个Case,每个test时Case都++,那么只要开一个数组判 ...

  4. AcceptAsync和BeginAccept的区别

    Difference between […]Async and Begin[…] .net asynchronous APIs Note that most *Async methods (with ...

  5. C#学习笔记(十六):索引器和重载运算符

    二维数组如何映射到一维数组 重载运算符 1.算术运算符 2.关系运算符, < 和 > 成对重载 using System; using System.Collections.Generic ...

  6. 【译】Asp.net core应用在 Kubernetes上内存使用率过高问题分析

    原文:https://blog.markvincze.com/troubleshooting-high-memory-usage-with-asp-net-core-on-kubernetes/ ps ...

  7. 分布式事务之——tcc-transaction分布式TCC型事务框架搭建与实战案例(基于Dubbo/Dubbox)

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/73731363 一.背景 有一定分布式开发经验的朋友都知道,产品/项目/系统最初为了 ...

  8. 列举一些有所帮助的blog和文章

    w3school hdoj imooc CSDN Github myGithub CmdMarkdown 一直在路上--记我从初中到本科近十年的学习成长历程 作者:周见智 C++中队列的建立与操作详细 ...

  9. POJ 2104 K-th Number(主席树模板题)

    http://poj.org/problem?id=2104 题意:求区间$[l,r]$的第k小. 思路:主席树不好理解啊,简单叙述一下吧. 主席树就是由多棵线段树组成的,对于数组$a[1,2...n ...

  10. UVa 1374 快速幂计算(dfs+IDA*)

    https://vjudge.net/problem/UVA-1374 题意:给出n,计算最少需要几次能让x成为x^n(x和已经生成的数相乘或相除). 思路:IDA*算法. 如果当前数组中最大的数乘以 ...