题意:有N家家店,每家店都对K种货物有需求;同时有M家仓库,对K钟货物有供应。对于每种货物,每个仓库送至每家店都有自己的单位费用。求满足所有店所有货物的最小费用

分析:对于每一种货物,如果总需求大于总费用,那么无解的;否则可以用KM匹配求解每一种货物的最小费用。

因为KM求的是最优匹配,所以建图的时候,建负权边即可。

将每一份需求和每一份供应都离散化,而不是把一个店的需求和每个一仓库的供应建立边。

#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn =;
const int INF= ; int w[maxn][maxn];
int m,n;//n左m右
int cx[maxn],cy[maxn];//顶标
bool usex[maxn],usey[maxn];//本回合使用的x,y
int link[maxn];//link[i]=x代表:在y图中的i与x相连 int A,B; bool dfs(int u){
usex[u]=;
for(int i=;i<=B;i++)
if(!usey[i]&&cx[u]+cy[i]==w[u][i]){
usey[i]=;
if(link[i]==-||dfs(link[i])){
link[i]=u;
return ;
}
}
return ;
}
int KM(){
memset(cy,,sizeof(cy));
memset(cx,-,sizeof(cx));
memset(link,-,sizeof(link));
for(int i=;i<=A;i++)
for(int j=;j<=B;j++)
cx[i]=max(cx[i],w[i][j]);
for(int i=;i<=A;i++){
while(){
int d=INF;
memset(usex,,sizeof(usex));
memset(usey,,sizeof(usey));
if(dfs(i))break;
for(int i=;i<=A;i++)
if(usex[i])
for(int j=;j<=B;j++)
if(!usey[j])d=min(d,cx[i]+cy[j]-w[i][j]);
if(d==INF)return -;
for(int i=;i<=A;i++)
if(usex[i])cx[i]-=d;
for(int i=;i<=B;i++)
if(usey[i])cy[i]+=d;
}
} int ans=;
for(int i=;i<=B;i++){
if(~link[i]){
ans -= w[link[i]][i];
}
}
return ans;
} int nd[maxn][maxn];
int sp[maxn][maxn];
int ct[maxn][maxn][maxn];
int cas; int cnt1[maxn],cnt2[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,tmp;
int u,v,k;
while(scanf("%d%d%d",&n,&m,&k)==){
if(!n && !m && !k) break; for(int i=;i<=n;++i){
for(int j=;j<=k;++j){
scanf("%d",&nd[i][j]);
}
} for(int i=;i<=m;++i){
for(int j=;j<=k;++j){
scanf("%d",&sp[i][j]);
}
} for(int i=;i<=k;++i){
for(int j=;j<=n;++j){
for(int t = ;t<=m;++t){
scanf("%d",&ct[i][j][t]);
}
}
} bool flag = true;
for(int cas=;cas<=k;++cas){
int need = ,supply =;
for(int i=;i<=n;++i) need += nd[i][cas];
for(int i=;i<=m;++i) supply += sp[i][cas];
if(need>supply){
flag = false;
break;
}
} if(!flag){
puts("-1");
continue;
} int tot = ;
for(int cas = ;cas<=k;++cas){
A=B=;
for(int i=;i<=n;++i){
for(int j=;j<=nd[i][cas];++j){
cnt1[++A] = i;
}
}
for(int i=;i<=m;++i){
for(int j=;j<=sp[i][cas];++j){
cnt2[++B] = i;
}
}
for(int i=;i<=A;++i){
for(int j=;j<=B;++j){
w[i][j] = -ct[cas][cnt1[i]][cnt2[j]];
}
}
int tmp = KM();
tot+=tmp;
}
printf("%d\n",tot);
}
return ;
}

POJ 2516 Minimum Cost (KM最优匹配)的更多相关文章

  1. POJ 2516 Minimum Cost (网络流,最小费用流)

    POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...

  2. Poj 2516 Minimum Cost (最小花费最大流)

    题目链接: Poj  2516  Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...

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

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

  4. POJ - 2516 Minimum Cost 每次要跑K次费用流

    传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...

  5. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  6. POJ 2516 Minimum Cost 最小费用流 难度:1

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 13511   Accepted: 4628 Des ...

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

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

  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. JQ 时间插件

    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/u ...

  2. 04 Java图形化界面设计——布局管理器之BorderLayout(边界布局)

    边界布局管理器把容器的的布局分为五个位置:CENTER.EAST.WEST.NORTH.SOUTH.依次对应为:上北(NORTH).下南(SOUTH).左西(WEST).右东(EAST),中(CENT ...

  3. C++三大函数(The Big Three)

    本文用Markdown编辑,这里对Markdown的支持不完善,更好的阅读体验请移步:我的Markdown文本 C++三大函数: 析构函数 复制构造函数 operator= 析构函数 函数模样:~S( ...

  4. 搭建一个简单的基于web的网络流量监控可视化系统

    本文转载于我的个人博客,转载请标明出处. 初衷 在腾讯云的学生认证申请提交上去n天之后,终于得到了审批,所以迫不及待的想玩玩腾讯云,作为一个搞网络的,自然有一些关于网络应用的小玩意,所以把以前部署过的 ...

  5. 《从零开始学Swift》学习笔记(Day 63)——Cocoa Touch设计模式及应用之单例模式

    原创文章,欢迎转载.转载请注明:关东升的博客 什么是设计模式.设计模式是在特定场景下对特定问题的解决方案,这些解决方案是经过反复论证和测试总结出来的.实际上,除了软件设计,设计模式也被广泛应用于其他领 ...

  6. Webservice工作原理及实例

    Web Service工作原理及实例   一.Web Service基本概念   Web Service也叫XML Web Service WebService是一种可以接收从Internet或者In ...

  7. (转)HTTP

    HTTP(Hyper Text Transfer Protocol)超文本传输协,是一个应用层协议,由请求和响应构成,是一个标准的客户端服务器模型. HTTP特点: 支持客户 / 服务器模式 简单快速 ...

  8. delphi xe---intraweb基本介绍

    版本10 seattle 新建intraWeb new->other->Delphi Projecs->IntraWeb->IntraWeb Application wizar ...

  9. Delphi xe---FIREDAC

    delphi  xe 10.2 完成FireDAC支持NoSQL MongoDB数据库,包括新FireDAC MongoDB,包括新FireDAC MongoDB的驱动.

  10. 数据库操作(使用FMDB)

    iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...