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 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

这一题的边有两个性质,

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

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

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

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

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

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

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

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

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=;
const int maxm=;
const int maxk=;
const int maxnum=;
const int inf =0x7fffffff; int f[maxnum][maxnum];//s 151 t 152
int cons[maxk][maxnum];
int cost[maxk][maxnum][maxnum];
int e[maxnum][maxnum];
int len[maxnum]; const int sups=,supt=;
int sum,n,m,k; int s1[maxk],s2[maxk];//s1 0-n-1 s2 n-n+m
bool input(){
memset(cons,,sizeof(cons));
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2)); sum=;
if(scanf("%d%d%d",&n,&m,&k)!=)return false;
if(n==&&m==&&k==)return false;
for(int i=;i<n;i++){
int c;
for(int j=;j<k;j++){
scanf("%d",&c);
cons[j][i]=c;
s1[j]+=c;
}
}
for(int i=n;i<n+m;i++){
int c;
for(int j=;j<k;j++){
scanf("%d",&c);
cons[j][i]=c;
s2[j]+=c;
}
} for(int i=;i<k;i++){
for(int j=;j<n;j++){
for(int ii=n;ii<n+m;ii++){
int c;
scanf("%d",&c);
cost[i][j][ii]=c;cost[i][ii][j]=-c;
}
}
} for(int i=;i<n;i++){
for(int j=n;j<n+m;j++){
e[i][j-n]=j;
e[j][i]=i;
e[i][m]=;
e[j][n]=;
e[][i]=i;
e[][j-n]=j;
}
}
fill(len,len+n,m+);
fill(len+n,len+n+m,n+);
len[]=n;
len[]=m;
return true;
}
void build(int kind){
memset(f,,sizeof(f));
for(int i=;i<n;i++)f[][i]=cons[kind][i];
for(int i=n;i<n+m;i++)f[i][]=cons[kind][i];
for(int i=;i<n;i++){
for(int j=n;j<n+m;j++){
f[i][j]=inf;
}
}
}
int d[maxnum],pre[maxnum];
bool vis[maxnum];
queue<int >que;
int mincostmaxflow(int s,int flow,int kind){
build(kind);
int res=;
while(flow>){
fill(d,d+,inf);
memset(vis,,sizeof(vis));
d[s]=;
que.push(s);
while(!que.empty()){
int fr=que.front();que.pop();
vis[fr]=false;
for(int i=;i<len[fr];i++){
int t=e[fr][i];
if(f[fr][t]>&&d[t]>d[fr]+cost[kind][fr][t]){
d[t]=d[fr]+cost[kind][fr][t];
pre[t]=fr;
if(!vis[t]){
que.push(t);
vis[t]=true;
}
}
}
}
if(d[supt]==inf)return -;
int sub=flow;
for(int v=supt;v!=sups;v=pre[v]){
sub=min(sub,f[pre[v]][v]);
}
flow-=sub;
res+=sub*d[supt];
for(int v=supt;v!=sups;v=pre[v]){
f[v][pre[v]]+=sub;
f[pre[v]][v]-=sub;
}
}
return res;
}
int main(){
while(input()){
int ans=;
bool sign=false;
for(int i=;i<k;i++){
if(s1[i]>s2[i]){printf("-1\n");sign=true;break;}
}
if(sign)continue;
for(int i=;i<k;i++){
int res=mincostmaxflow(,s1[i],i);
if(res==-){printf("-1\n");sign=true;break;}
ans+=res;
}
if(sign)continue;
printf("%d\n",ans); }
return ;
}

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. VC++开机自动启动程序的几种方法 (转载)

    转载:http://blog.csdn.net/zhazhiqiang/article/details/51909703 很多监控软件要求软件能够在系统重新启动后不用用户去点击图标启动项目,而是直接能 ...

  2. The destination you provided is not a full refname (i.e., starting with "refs/")

    $ git push v5 v5/hotfix/5.1:hotfix/5.1-quartzerror: The destination you provided is not a full refna ...

  3. UVA 11806 Cheerleaders (容斥原理

    1.题意描述 本题大致意思是讲:给定一个广场,把它分为M行N列的正方形小框.现在给定有K个拉拉队员,每一个拉拉队员需要站在小框内进行表演.但是表演过程中有如下要求: (1)每一个小框只能站立一个拉拉队 ...

  4. HBase底层存储原理——我靠,和cassandra本质上没有区别啊!都是kv 列存储,只是一个是p2p另一个是集中式而已!

    理解HBase(一个开源的Google的BigTable实际应用)最大的困难是HBase的数据结构概念究竟是什么?首先HBase不同于一般的关系数据库,它是一个适合于非结构化数据存储的数据库.另一个不 ...

  5. Gym - 100342J Triatrip (bitset求三元环个数)

    https://vjudge.net/problem/Gym-100342J 题意:给出一个邻接矩阵有向图,求图中的三元环的个数. 思路: 利用bitset暴力求解,记得最后需要/3. #includ ...

  6. HTTP 随笔

    浏览器发送HTTP请求主要分为三部分请求行,Response Headers(响应头信息)和Request Headers(请求头信息). 请求行有分为三部分:请求方法,请求路径和请求协议 请求方法有 ...

  7. Jmeter自动化测试 数据驱动测试,将数据存入csv文件中来调用,或将数据存在DB中进行调用

    1. 将测试的用例名称,测试请求方式,测试链接,预置数据,断言等都放到excel中,然后转成csv格式,在用Jmeter带的csv数据配置文件导入 运行之前将线程组中配置,线程数设置为1,循环的次数设 ...

  8. MongoDB(课时9 范围运算)

    3.2.2.4 范围查询 只要是数据库,必须存在有“$in”(在范围之中).“$nin”(不在范围之中). 范例:查询姓名是“张三”,“李四”,“王五” db.students.find({" ...

  9. 中文名文件上传到linux服务器上以后文件名会乱码(openoffice)

    1.中文名文件上传后保存在linux服务器上文件名会乱码,但是我们通过SSH直接对服务器上的一个文件进行重命名是可以使用中文的,而且显示出来是正确的,这说明服务器是可以支持中文的. 2.而为什么上传的 ...

  10. [源][osg][osgBullet]osgBullet例子介绍

    1.BasicDemo 基本样例 一个小玩具飞机坠落,一个立方体盒子在转圈 2.centerofmass 质心 3.collision 碰撞检测 这里有一个大神自己改的例子: http://blog. ...