题意:

有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. Django model 中的字段解释

    Django 通过 models 实现数据库的创建.修改.删除等操作,本文为模型中一般常用的类型的清单,便于查询和使用: AutoField:一个自动递增的整型字段,添加记录时它会自动增长.你通常不需 ...

  2. ceph PG数量调整/PG的状态说明

    优化: PG Number PG和PGP数量一定要根据OSD的数量进行调整,计算公式如下,但是最后算出的结果一定要接近或者等于一个2的指数.调整PGP不会引起PG内的对象的分裂,但是会引起PG的分布的 ...

  3. mvc 中英文切换

    我常用的2个方案,其实性质是一样的 方案1,使用过滤器 ActionFilterAttribute,这个就不细说了,比较方便. 实现一个继承自ActionFilterAttribute的类,实现OnA ...

  4. BZOJ 3164: [Heoi2013]Eden的博弈问题

    3164: [Heoi2013]Eden的博弈问题 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 134  Solved: 98[Submit][St ...

  5. 设置outlook 2013 默认的ost路径

    How To Change Default Data File (.OST) Location in Office 2013 To set the default location of an out ...

  6. WinRM不起作用 Connecting to remote server failed with the following error message : WinRM cannot complete the operation

    当我运行下面的 powershell  脚本时: $FarmAcct = 'domain\user'  $secPassword = ConvertTo-SecureString 'aaa' -AsP ...

  7. 洛谷 P3962 [TJOI2013]数字根 解题报告

    P3962 [TJOI2013]数字根 题意 数字根:这个数字每一位的数字加起来求和,反复这个过程直到和小于10. 给出序列\(a\),询问区间\([l,r]\)连续的子区间里最大前5个不同的数字根, ...

  8. 修复Mysql主从不同步shell

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

  9. Fence Repair(poj3253)

    题目链接:http://poj.org/problem?id=3253 Description Farmer John wants to repair a small length of the fe ...

  10. pacman安装软件包出现损坏

    状况 File .pkg.tar.xz is corrupted (invalid or corrupted package (PGP signature)).Do you want to delet ...