题意:

有N个客户,M个仓库,和K种货物。已知每个客户需要每种货物的数量,每个仓库存储每种货物的数量,每个仓库运输各种货物去各个客户的单位费用。判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用。

思路:

最小费用最大流。先判断是否每种货物的存储总量都足够,足够的话,对每一种货物进行一次最小费用最大流求出完成这种货物运输的最小总费用,所有的总费用相加就是结果了。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring> using namespace std;
const int maxn = ;
const int inf = ; int n, ans;
int cap[maxn][maxn], pre[maxn];
int cost[maxn][maxn], dis[maxn];
int que[maxn];
bool vis[maxn]; bool spfa()
{
int i, head = , tail = ;
for(i = ; i <= n; i ++){
dis[i] = inf;
vis[i] = false;
}
dis[] = ;
que[] = ;
while(tail != head){
int u = que[head];
vis[u] = true;
for(i = ; i <= n; i ++)
if(cap[u][i] && dis[i] > dis[u] + cost[u][i]){
dis[i] = dis[u] + cost[u][i];
pre[i] = u;
if(!vis[i]){
vis[i] = true;
que[tail ++] = i;
if(tail == maxn) tail = ;
}
}
vis[u] = false;
head ++;
if(head == maxn) head = ;
}
if(dis[n] == inf) return false;
return true;
} void end()
{
int i, sum = inf;
for(i = n; i != ; i = pre[i])
sum = min(sum, cap[pre[i]][i]);
for(i = n; i != ; i = pre[i]){
cap[pre[i]][i] -= sum;
cap[i][pre[i]] += sum;
ans += cost[pre[i]][i] * sum;
}
} int main()
{
int N, M, K, i, j, k;
int need[maxn][maxn], NeedK[maxn];
int have[maxn][maxn], HaveK[maxn];
while(cin>>N>>M>>K,N,M,K)
{
memset(NeedK, , sizeof(NeedK));
for(i = ; i <= N; i ++)
for(j = ; j <= K; j ++){
scanf("%d", &need[i][j]); // 第i个客户需要第j种货物的量。
NeedK[j] += need[i][j]; // 第j种货物总共需要的量。
}
memset(HaveK, , sizeof(HaveK));
for(i = ; i <= M; i ++)
for(j = ; j <= K; j ++){
scanf("%d", &have[i][j]); // 第i个仓库存储第j种货物的量。
HaveK[j] += have[i][j]; // 第j种货物总共需要的量。
}
bool flag = true;
for(i = ; i <= K; i ++) // 判断所有货物是否足够。
if(NeedK[i] > HaveK[i]){
flag = false; break;
}
ans = ;
n = N + M + ;
for(k = ; k <= K; k ++){
memset(cap, , sizeof(cap));
for(i = ; i <= N; i ++) // 建图。
for(j = ; j <= M; j ++){
scanf("%d", &cost[j][M+i]);
cost[M+i][j] = -cost[j][M+i];
cap[j][M+i] = inf;
}
if(!flag) continue;
for(i = ; i <= M; i ++){
cap[][i] = have[i][k];
cost[][i] = cost[i][] = ;
}
for(i = ; i <= N; i ++){
cap[M+i][n] = need[i][k];
cost[M+i][n] = cost[n][M+i] = ;
}
while(spfa()) end(); // 最小费用最大流算法。
}
if(flag) cout << ans << endl;
else cout << - << endl;
}
return ;
}

POJ2516 Minimum Cost【最小费用最大流】的更多相关文章

  1. POJ2516 Minimum Cost —— 最小费用最大流

    题目链接:https://vjudge.net/problem/POJ-2516 Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Tota ...

  2. POJ2516:Minimum Cost(最小费用最大流)

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19088   Accepted: 6740 题目链 ...

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

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

  4. POJ 2516 Minimum Cost [最小费用最大流]

    题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...

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

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

  6. POJ2516 Minimum Cost(最小费用最大流)

    一开始我把每个店主都拆成k个点,然后建图..然后TLE.. 看题解= =哦,愚钝了,k个商品是独立的,可以分别跑k次最小费用最大流,结果就是k次总和.. #include<cstdio> ...

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

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

  8. poj-2516.minimum cost(k次费用流)

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19883   Accepted: 7055 Des ...

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

    1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...

随机推荐

  1. Hdoj 1008.Elevator 题解

    Problem Description The highest building in our city has only one elevator. A request list is made u ...

  2. android-support-v4.jar 免积分下载

    资源名称:android扩展插件 android-support-v4.jar 资源大小:137KB 上传日期:2012-10-08 资源积分:1 下载次数:136 电信下载地址:http://www ...

  3. 【BZOJ2245】[SDOI2011]工作安排(费用流)

    [BZOJ2245][SDOI2011]工作安排(费用流) 题面 BZOJ 洛谷 题解 裸的费用流吧. 不需要拆点,只需要连边就好了,保证了\(W_j<W_{j+1}\). #include&l ...

  4. 修复Mysql主从不同步shell

    使用第三方工具MySQL Enterprise Monitor,MySQL企业版监控工具.MONyog – MySQL Monior and Advisor,MONyog大家都不陌生,windows下 ...

  5. 设计模式 (二)——观察者模式(Observer,行为型)

    1.概述 使用设计模式可以提高代码的可复用性.可扩充性和可维护性.观察者模式(Observer Pattern)属于行为型模式,在对象之间定义一对多的依赖,这样一来,当一个对象改变状态,依赖它的对象都 ...

  6. Centos6.5 防火墙开放端口

    0. 说明 centos6.5处于对安全的考虑,严格控制网络进去.所以在安装mysql或者使用tomcat,需要开放端口3306或8080. 通常的解决办法有两个.一个是直接关闭防火墙(非常不推荐): ...

  7. Ubuntu16.04创建electronic-wechat启动器图标

    步骤 将最新的electronic-wechat二进制包,下载解压,并移动到/opt/下面.sudo cp -r electronic-wechat/ /opt/electronic-wechat/ ...

  8. 几个简单常用的Sql语句

    '; --查Cids为2的Gnumber列的和,列名为Ids select Cids,Plevel from People; select * from Salary; select * from S ...

  9. A1078. Hashing

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  10. gradle下载的依赖包位置 及 修改

    gradle下载的依赖包位置 2018年08月01日 00:37:06 LuckyJiang-2019 阅读数:3569   Mac系统默认下载到: /Users/(用户名)/.gradle/cach ...