POJ 2516 Minimum Cost
每个物品分开做最小费用最大流。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+;
const int INF=0x7FFFFFFF;
struct Edge
{
int from,to,cap,flow,cost;
};
int n,m,len,s,t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
int Flag,Ans,Xu;
int N,M,K;
int People[][];
int Supply[][];
int Cost[][][]; void init()
{
for(int i=; i<maxn; i++) G[i].clear();
edges.clear();
} void Addedge(int from,int to,int cap,int cost)
{
edges.push_back((Edge)
{
from,to,cap,,cost
});
edges.push_back((Edge)
{
to,from,,,-cost
});
len=edges.size();
G[from].push_back(len-);
G[to].push_back(len-);
} bool BellmanFord(int s,int t,int &flow,int &cost)
{ for(int i=; i<maxn; i++) d[i]=INF; memset(inq,,sizeof(inq));
memset(p,-,sizeof(p)); d[s]=;
inq[s]=;
p[s]=;
a[s]=INF; queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=;
for(int i=; 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]=;
}
}
}
}
if(d[t]==INF) return false;
flow+=a[t];
cost+=d[t]*a[t];
int u=t;
while(u!=s)
{
edges[p[u]].flow+=a[t];
edges[p[u]^].flow-=a[t];
u=edges[p[u]].from;
}
return true;
} void Mincost (int s,int t)
{
int flow=,cost=;
while(BellmanFord(s,t,flow,cost));
if(flow!=Xu) Flag=;
else Ans=Ans+cost;
} int main()
{
while(~scanf("%d%d%d",&N,&M,&K))
{
if(!N&&!M&&!K) break;
Flag=;
Ans=;
for(int i=; i<=N; i++)
for(int j=; j<=K; j++)
scanf("%d",&People[i][j]);
for(int i=; i<=M; i++)
for(int j=; j<=K; j++)
scanf("%d",&Supply[i][j]);
for(int k=; k<=K; k++)
for(int i=; i<=N; i++)
for(int j=; j<=M; j++)
scanf("%d",&Cost[k][i][j]);
for(int k=; k<=K; k++)
{
init();s=;t=M+M+N+;Xu=;
for(int i=; i<=M; i++) Addedge(s,i,INF,);
for(int i=; i<=M; i++) Addedge(i,i+M,Supply[i][k],);
for(int i=; i<=N; i++)
for(int j=; j<=M; j++)
Addedge(j+M,M+M+i,INF,Cost[k][i][j]);
for(int i=; i<=N; i++) Addedge(i+M+M,t,People[i][k],);
for(int i=; i<=N; i++) Xu=Xu+People[i][k];
Mincost(s,t);
if(!Flag) break;
}
if(Flag) printf("%d\n",Ans);
else printf("-1\n");
}
return ;
}
POJ 2516 Minimum Cost的更多相关文章
- POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
- Poj 2516 Minimum Cost (最小花费最大流)
题目链接: Poj 2516 Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...
- POJ 2516 Minimum Cost (最小费用最大流)
POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...
- POJ - 2516 Minimum Cost 每次要跑K次费用流
传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- POJ 2516 Minimum Cost 最小费用流 难度:1
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 13511 Accepted: 4628 Des ...
- POJ 2516 Minimum Cost(最小费用流)
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- POJ 2516 Minimum Cost(拆点+KM完备匹配)
题目链接:http://poj.org/problem?id=2516 题目大意: 第一行是N,M,K 接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况 接下来M行:第j行有K个数字表示 ...
- POJ 2516 Minimum Cost [最小费用最大流]
题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...
- 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个 ...
随机推荐
- concurrent实用类
improt java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.RejectedExecutionHandler; ...
- perl Mail::Sender模块发送邮件
#!/usr/bin/perl -w use strict; use Mail::Sender; ; ){ my $sender = Mail::Sender->new({ smtp => ...
- PHP之APC缓存详细介绍(学习整理)
APC的介绍 The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provi ...
- 判断字符串是否为UTF8编码
UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码.由Ken Thompson于1992年创建.现在已经标准化为RFC 36 ...
- eclipse保存时自动格式化代码和优化导包
- Python统计栏目页面数量
主要为了装个B,统计栏目页面数量时候用的,多线程基本照抄 http://www.cnblogs.com/fnng/p/3670789.html 关于对SEO有什么用处...我觉得...仅对本人有用,P ...
- ios根据文本自适应 然后 搭建类似如下效果
UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tbSecond.size.width, 0)]; hea ...
- kali自动化清理缓存和日志
前几天发现kali无法正常开机,启动盘启动进去之后, 发现/var/log/目录下的日志文件太大把硬盘占满了,于是乎... 文件主要是这三个: kern.log : 包含内核产生的日志,有助于在定 ...
- js-学习方法之3
熟悉JavaScript每一个方法的作用 这一要求听起来似乎有点不太实际,我想这个要求对于像C#.JAVA这些大型语言来说确实是,因为这些语言类库实在太庞大了,相信没有人可以全面记住它,而且也是没有必 ...
- 2016 SyScan360 国际前瞻信息安全会议 多角度探讨信息安全
SyScan360国际前瞻信息安全会议由与中国第一大互联网安全公司-360公司与SyScan前瞻信息安全技术年会(TheSymposiumonSecurityforAsiaNetwork,以下简称Sy ...