题目链接:https://vjudge.net/problem/POJ-2516

Minimum Cost
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 17650   Accepted: 6205

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

Source

题意:

有n个商店,m个仓库,k中商品。每个商店对每种商品都有特定需求量,且每个仓库中,每种商品都有其特定的存量。且已知对于某一种商品G,从仓库A运送一件商品G到商店B的运费。问:能否满足所有商店的供货需求?如果能满足,求出最小总运费?

题解:

最小费用最大流问题。可知每一种商品是相互独立的,因此我们可以单独求出每种商品总的最小运费,然后加起来,当然前提条件是能满足需求。

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cmath>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. const int INF = 2e9;
  15. const LL LNF = 9e18;
  16. const int mod = 1e9+;
  17. const int MAXM = 1e4+;
  18. const int MAXN = 1e2+;
  19.  
  20. struct Edge
  21. {
  22. int to, next, cap, flow, cost;
  23. }edge[MAXM<<];
  24. int tot, head[MAXN];
  25. int pre[MAXN], dis[MAXN];
  26. bool vis[MAXN];
  27. int N;
  28.  
  29. void init(int n)
  30. {
  31. N = n;
  32. tot = ;
  33. memset(head, -, sizeof(head));
  34. }
  35.  
  36. void add(int u, int v, int cap, int cost)
  37. {
  38. edge[tot].to = v; edge[tot].cap = cap; edge[tot].cost = cost;
  39. edge[tot].flow = ; edge[tot].next = head[u]; head[u] = tot++;
  40. edge[tot].to = u; edge[tot].cap = ; edge[tot].cost = -cost;
  41. edge[tot].flow = ; edge[tot].next = head[v]; head[v] = tot++;
  42. }
  43.  
  44. bool spfa(int s, int t)
  45. {
  46. queue<int>q;
  47. for(int i = ; i<N; i++)
  48. {
  49. dis[i] = INF;
  50. vis[i] = false;
  51. pre[i] = -;
  52. }
  53.  
  54. dis[s] = ;
  55. vis[s] = true;
  56. q.push(s);
  57. while(!q.empty())
  58. {
  59. int u = q.front();
  60. q.pop();
  61. vis[u] = false;
  62. for(int i = head[u]; i!=-; i = edge[i].next)
  63. {
  64. int v = edge[i].to;
  65. if(edge[i].cap>edge[i].flow && dis[v]>dis[u]+edge[i].cost)
  66. {
  67. dis[v] = dis[u]+edge[i].cost;
  68. pre[v] = i;
  69. if(!vis[v])
  70. {
  71. vis[v] = true;
  72. q.push(v);
  73. }
  74. }
  75. }
  76. }
  77. if(pre[t]==-) return false;
  78. return true;
  79. }
  80.  
  81. int minCostMaxFlow(int s, int t, int &cost)
  82. {
  83. int flow = ;
  84. cost = ;
  85. while(spfa(s,t))
  86. {
  87. int Min = INF;
  88. for(int i = pre[t]; i!=-; i = pre[edge[i^].to])
  89. {
  90. if(Min>edge[i].cap-edge[i].flow)
  91. Min = edge[i].cap-edge[i].flow;
  92. }
  93. for(int i = pre[t]; i!=-; i = pre[edge[i^].to])
  94. {
  95. edge[i].flow += Min;
  96. edge[i^].flow -= Min;
  97. cost += edge[i].cost*Min;
  98. }
  99. flow += Min;
  100. }
  101. return flow;
  102. }
  103.  
  104. int need[][], storage[][], fee[][][];
  105. int main()
  106. {
  107. int n, m, k;
  108. while(scanf("%d%d%d",&n,&m,&k)&&(n||m||k))
  109. {
  110. for(int i = ; i<=n; i++)
  111. for(int j = ; j<=k; j++)
  112. scanf("%d", &need[i][j]);
  113.  
  114. for(int i = ; i<=m; i++)
  115. for(int j = ; j<=k; j++)
  116. scanf("%d", &storage[i][j]);
  117.  
  118. for(int i = ; i<=k; i++)
  119. for(int j = ; j<=n; j++)
  120. for(int t = ; t<=m; t++)
  121. scanf("%d", &fee[i][j][t]);
  122.  
  123. int cost = ;
  124. for(int item = ; item<=k; item++)
  125. {
  126. int whole = ;
  127. for(int i = ; i<=n; i++)
  128. whole += need[i][item];
  129.  
  130. int start = , end = n+m+;
  131. init(n+m+);
  132. for(int i = ; i<=m; i++)
  133. {
  134. add(start, i, storage[i][item], );
  135. for(int j = ; j<=n; j++)
  136. // if(storage[i][item]>=need[j][item]) //不能加此条判断
  137. add(i, m+j, storage[i][item], fee[item][j][i]);
  138. }
  139.  
  140. for(int i = ; i<=n; i++)
  141. add(m+i, end, need[i][item], );
  142.  
  143. int cost_item;
  144. int offered = minCostMaxFlow(start, end, cost_item);
  145. if(offered<whole)
  146. {
  147. cost = -;
  148. break;
  149. }
  150. else cost += cost_item;
  151. }
  152. printf("%d\n", cost);
  153. }
  154. }

POJ2516 Minimum Cost —— 最小费用最大流的更多相关文章

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

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

  2. Minimum Cost(最小费用最大流)

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

  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. POJ2516 Minimum Cost(最小费用最大流)

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

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

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

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

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

  8. poj-2516.minimum cost(k次费用流)

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19883   Accepted: 7055 Des ...

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

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

随机推荐

  1. NAND FLASH 物理结构分析

    转自:http://blog.51cto.com/hardywang/2053915 NAND Flash是一种非易失性随机访问存储介质,基于浮栅(Floating Gate)晶体管设计,通过浮栅来锁 ...

  2. 仓库建设(bzoj 1096)

    Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天, ...

  3. windows创建任务计划(周期执行bat脚本)

    https://jingyan.baidu.com/article/ca00d56c767cfae99febcf73.html windows找到任务计划程序: 这台电脑->管理

  4. go gin框架 static 静态文件

    项目结构: DemoPro: .. .. static/ test.txt .. router := gin.Default() router.Static("/static", ...

  5. hdu 1827 有向图缩点看度数

    题意:给一个有向图,选最少的点(同时最小价值),从这些点出发可以遍历所有. 思路:先有向图缩点,成有向树,找入度为0的点即可. 下面给出有向图缩点方法: 用一个数组SCC记录即可,重新编号,1.... ...

  6. SGU101 求有重边的无向图欧拉迹

    题意:好多木棒,俩端有数字(0--6)标记,按数字相同的端首尾相连成一条直线(木棒可以相同).即求有重边的无向图欧拉迹. 先判定是否为欧拉图,俩个条件,不说了.如果是欧拉图,输出路经. 方法:dfs遍 ...

  7. 通过Nginx 的反向代理来加强kibana的访问安全

    https://blog.csdn.net/choelea/article/details/57406086

  8. android连数据库

    package com.rockcheck.mes; import android.os.AsyncTask; import android.support.v7.app.AppCompatActiv ...

  9. Windows下Python安装pyecharts

    都说pyechart用来可视化好,可是安装的时候各种坑 正常情况是 pip install pyecharts 然后各种报错,找到一种可行的方式 在https://pypi.org/project/p ...

  10. 转: 性能测试应该怎么做? (from coolshell.cn)

    转自: http://coolshell.cn/articles/17381.html 偶然间看到了阿里中间件Dubbo的性能测试报告,我觉得这份性能测试报告让人觉得做这性能测试的人根本不懂性能测试, ...