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个 ...
随机推荐
- docker多主机网络方案
本文探讨Docker多主机网络的性能. 在过去的博文里,我测试过 Docker的网络 . MySQL服务器团队 提供了他们自己的结果,和我的观察是一致的. 本文里一系列的测试,想更多关注使用多主机的D ...
- CVE-2015-5122 简要分析(2016.4)
CVE-2015-5122 简要分析 背景 最近在学习Flash漏洞的分析,其与IE漏洞的分析还是有诸多的不同(不便)之处,折腾了一阵子终于克服了没有符号表.Flash的超时定时器等问题.所以找到了去 ...
- JavaScript DOM编程艺术-学习笔记(第八章、第九章)
第八章 1.小知识点: ①某些浏览器要根据DOCTYPE 来决定页面的呈现模式(标准模式 / 怪异模式--也称兼容模式): 兼容模式意味着浏览器要模仿老一辈的浏览器的怪异行为,来让老站点得到运行,并让 ...
- C#键盘事件处理
键盘事件是在用户按下键盘上的一个键的时候发生的,可分为两类.第一类是KeyPress事件,当按下的键表示的是一个ASCII字符的时候就会触发这类事件,可通过他的KeyPressEventArgs类型参 ...
- visual SVN 反编译破解
今天发现visual SVN 过期了.网上搜索了一下,发现了下面的贴子. http://www.heiqu.com/show-71200-1.html 一看是用.Net写的,大喜,破解就太简单了.本来 ...
- 残酷中蜕变的SEOer
SEO在不断蜕变,今天的自己推翻昨天的自己. 大牛说"SEOer们走的路不同,最终都会殊途同归." 最后都会炼出"SEO的道"最终都会回归到网站基础工程.毕 ...
- vim 替换
摘自: vim替换命令 替換(substitute) :[range]s/pattern/string/[c,e,g,i] range 指的是範圍,1,7 指從第一行至第七行,1,$ 指從第一行至最後 ...
- 【Python@Thread】queue模块-生产者消费者问题
python通过queue模块来提供线程间的通信机制,从而可以让线程分项数据. 个人感觉queue就是管程的概念 一个生产者消费者问题 from random import randint from ...
- 使用Slua框架开发Unity项目的重要步骤
下载与安装 下载地址 GitHub 安装过程1.下载最新版,这里, 解压缩,将Assets目录里的所有内容复制到你的工程中,对于最终产品,可以删除slua_src,例子,文档等内容,如果是开发阶段则无 ...
- Objetive-C initialize研究
initialize执行时机 在向一个类执行实例方法或者类方法(除了initialize和load方法之外)的时候,触发initialize方法,因此如果一个类你没有使用的时候,是 ...