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个 ...
随机推荐
- 敏捷开发之Scrum
现在敏捷开发是越来越火了,人人都在谈敏捷,人人都在学习Scrum和XP... 为了不落后他人,于是我也开始学习Scrum,今天主要是对我最近阅读的相关资料,根据自己的理解,用自己的话来讲述Scrum中 ...
- laravel创建定时任务
官方文档给出的教程已经很详细了,这里给出一些补充帮助大家理解. 英文文档:https://laravel.com/docs/5.2/scheduling 中文文档:https://laravel-ch ...
- B/S和C/S【转载Jane的博客 http://blog.sina.com.cn/liaojane】
什么是C/S和B/S结构? C/S又称Client/Server或客户/服务器模式.服务器通常采用高性能的PC.工作站或小型机,并采用大型数据库系统,如Oracle.Sybase.In ...
- 一.Maven的安装和配置整理
Maven的安装和配置 1.1安装 进入Maven官网的下载页面:http://maven.apache.org/download.cgi选择当前最新版本:" ...
- 从安装.net Core 到helloWord(Mac上)
最近听说微软 正式发不了.netCore 1.0 于是就安装了 并安装了vs Code 用于编写一些.net程序 一. .netCore的安装: 首先需要通过brew安装openssl(相信homeB ...
- 安装linux工作环境
1,介绍Vagrant 我们做web开发的时候经常要安装各种本地测试环境,比如apache,php,mysql,redis等等.出于个人使用习惯,可能我们还是比较习惯用windows.虽然说在wind ...
- C#多线程:深入了解线程同步lock,Monitor,Mutex,同步事件和等待句柄(中)
本篇继续介绍WaitHandler类及其子类 Mutex,ManualResetEvent,AutoResetEvent的用法..NET中线程同步的方式多的让人看了眼花缭乱,究竟该怎么去理解呢?其实, ...
- 安装Postgresql踩过的坑
PG安装相关的 1 系统语言的设置 PG的安装,和系统的locale设置有一定的关系,需要设置,如在.profile或.bashrc中 也可以运行下面的命令: dpkg-reconfigure lo ...
- Windows后渗透
My 命令行下收集主机信息 使用wmic识别安装到系统中的补丁情况: wmic qfe get description,installedOn 识别正在运行的服务: sc query type= se ...
- ESPlatform 支持的三种群集模型 —— ESFramework通信框架 4.0 进阶(09)
对于最多几千人同时在线的通信应用,通常使用单台服务器就可以支撑.但是,当同时在线的用户数达到几万.几十万.甚至百万的时候,我们就需要很多的服务器来分担负载.但是,依据什么规则和结构来组织这些服务器,并 ...