poj-2516.minimum cost(k次费用流)
Minimum Cost
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 19883 | Accepted: 7055 |
Description
It's known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places' storage of K kinds of goods, N shopkeepers' order of K kinds of goods and the cost to transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.
Input
Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper.
The input is terminated with three "0"s. This test case should not be processed.
Output
Sample Input
1 3 3
1 1 1
0 1 1
1 2 2
1 0 1
1 2 3
1 1 1
2 1 1 1 1 1
3
2
20 0 0 0
Sample Output
4
-1
Source
都在代码里了,不不建议抄袭代码,代码里有些调试代码,有需要的可以看代码之前注释,前面是解释,精髓在最后三行。
/*
本题心得:一开始做题就有种感觉需要对商品拆点,然后满足每个商人,但是这样的话每个商品要拆为n个点,必然会有很大的空间浪费造成tle,
实在没思路之后看了博客,看到说每种商品都是独立的,意思就是把商人需要的每种物品都单独买,然后统计最后结果就行了,这里有一个细节就是,
因为目的是满足所有商人情况下的最小费用,那也就是最小费用最大流,所以我们事先判断某种商品是否够用,如果够用,那么最大流一定是满载的,
所以不必担心找不到最大流,就找最小花费就行了。
对于每个商品,我们记得要清空head数组,额贼,这个把我坑了好久,后来想如果不清空必然会在spfa中造成无限循环(想想为什么?),所以对每件
商品都需要init,对于每件商品,我们建立超级源点指向那些供应商,容量为最大供应数目花费为0,对于每个商人,我们建立一条边指向超级汇点,容量为商人
对这件商品的需求数目(限制每个商人得到的物品数),花费为0,对于每个供应商和他的商人之间建立一条由供应商指向商人的边,cap为inf(由于前面我们已经限制了每个供应商可以提供的物品)
花费为这个供应商对这个商人供应这件物品的cost,跑一波费用流就ojk了。
这样我们就
通过供应商 -> 商人 限制了价格
通过 s -> 供应商 限制了供应个数
通过商人 -> t 限制了商人的需求数目。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int maxn = + , maxm = 1e4 + , inf = 0x3f3f3f3f;
int want[maxn][maxn], supply[maxn][maxn], sumwant[maxn], sumsupply[maxn], costij[maxn][maxn][maxn];
struct Edge {
int to, next, cap, flow, cost, from;
} edge[maxm];
int head[maxn << ], tot;
int pre[maxn << ], dis[maxn << ];
bool vis[maxn << ]; int N; void init(int n) {
N = n;
tot = ;
memset(head, -, sizeof head);
} void addedge(int u, int v, int cap, int cost) {
edge[tot].to = v; edge[tot].cap = cap; edge[tot].cost = cost; edge[tot].flow = ; edge[tot].from = u;
edge[tot].next = head[u]; head[u] = tot ++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].cost = -cost; edge[tot].flow = ; edge[tot].from = v;
edge[tot].next = head[v]; head[v] = tot ++;
} bool spfa(int s, int t) {
queue <int> que;
// memset(dis, inf, sizeof dis);
// memset(vis, false, sizeof vis);
// memset(pre, -1, sizeof pre);
for(int i = ; i <= N; i ++) {
dis[i] = inf;
vis[i] = false;
pre[i] = -;
}
dis[s] = ;
vis[s] = true;
que.push(s);
while(!que.empty()) {
// printf("in bfs");
int u = que.front();
que.pop();
vis[u] = false;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) {
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if(!vis[v]) {
vis[v] = true;
// printf("now in push of bfs");
que.push(v);
}
}
}
}
return ~pre[t];
// if(pre[t] == -1) return false;
// else return true;
} int mincostmaxflow(int s, int t) {
int cost = ;
while(spfa(s, t)) {
// printf("spfa is true");
int Min = inf;
for(int i = pre[t]; ~i; i = pre[edge[i ^ ].to]) {
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
// printf("now is find min");
}
for(int i = pre[t]; ~i; i = pre[edge[i ^ ].to]) {
edge[i].flow += Min;
edge[i ^ ].flow -= Min;
cost += edge[i].cost * Min;
// printf("now is update");
}
}
return cost;
} int main() {
int n, m, k;
while(~scanf("%d %d %d", &n, &m, &k) && (n | m | k)) { memset(want, , sizeof want);
memset(supply, , sizeof supply);
memset(sumwant, , sizeof sumwant);
memset(sumsupply, , sizeof sumsupply);
for(int i = ; i <= n; i ++) {
for(int j = ; j <= k; j ++) {
scanf("%d", &want[i][j]);
sumwant[j] += want[i][j];
}
}
for(int i = ; i <= m; i ++) {
for(int j = ; j <= k; j ++) {
scanf("%d", &supply[i][j]);
sumsupply[j] += supply[i][j];
}
}
bool flag = true;
for(int i = ; i <= k; i ++) {
if(sumwant[i] > sumsupply[i]) {
flag = false;
break;
}
}
for(int q = ; q <= k; q ++) {
for(int i = ; i <= n; i ++) {
for(int j = ; j <= m; j ++) {
scanf("%d", &costij[q][i][j]);//第q件物品,第i个人从第j个供应商的花费
}
}
}
int s = , t = m + n + , mcmf = ;
if(flag) {
for(int q = ; q <= k; q ++) {
init(n + m + );
// printf("***************\n");
for(int i = ; i <= m; i ++) {
addedge(s, i, supply[i][q], );
}
for(int i = ; i <= n; i ++) {
addedge(i + m, t, want[i][q], );
}
for(int i = ; i <= n; i ++) {
for(int j = ; j <= m; j ++) {
addedge(j, i + m, inf, costij[q][i][j]);
}
}
// for(int i = 0; i < tot; i ++) {
// printf("%d -> %d\n", edge[i].from, edge[i].to);
// }
mcmf += mincostmaxflow(s, t);
}
printf("%d\n", mcmf);
} else printf("-1\n");
}
return ;
}
poj-2516.minimum cost(k次费用流)的更多相关文章
- 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 (最小花费最大流)
题目链接: Poj 2516 Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...
- POJ - 2516 Minimum Cost(最小费用最大流)
1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...
- POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
- 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 每次要跑K次费用流
传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...
- 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 最小费用流
题目: 给出n*kk的矩阵,格子a[i][k]表示第i个客户需要第k种货物a[i][k]单位. 给出m*kk的矩阵,格子b[j][k]表示第j个供应商可以提供第k种货物b[j][k]单位. 再给出k个 ...
- POJ 2516 Minimum Cost
每个物品分开做最小费用最大流. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
随机推荐
- uboot if_changed函数
u-boot编译过程分析 u-boot.lds: $(LDSCRIPT) prepare FORCE $(call if_changed_dep,cpp_lds) u-boot: $(u-boot-i ...
- babel 转换箭头函数
转换前: const sum = (a,b)=>a+b 转化后: // "use strict"; // var fn = function fn(a, b) { // re ...
- react native 之 redux 使用套路
redux是什么?他是一个state容器 redux的运作方式是怎样的? 接入方式: 1. npm install 下列内容: npm install --save redux npm install ...
- Java框架之MybatisSQL注入漏洞
一.SQL注入漏洞基本原理 在常见的web漏洞中,SQL注入漏洞较为常见,危害也较大.攻击者一旦利用系统中存在的SQL注入漏洞来发起攻击,在条件允许的情况下,不仅可以获取整站数据,还可通过进一步的渗透 ...
- docker 安装MongoDB以及设置用户
MongoDB 是一个免费的开源跨平台面向文档的 NoSQL 数据库程序. 1.查看可用的 MongoDB 版本 访问 MongoDB 镜像库地址: https://hub.docker.com/_/ ...
- (24)Python实现递归生成或者删除一个文件目录及文件
def removeDir(dirPath): ''' Created by Wu Yongcong 2017-8-18 :param dirPath: :return: ''' if not os. ...
- 测试常用linux命令1
进程相关: 1,查看所有进程(包含历史进程): ps -ef 各个参数的含义依次是uid,pid,ppid,c(cpu利用率),stime(进程启动时间),tty,time,cmd 2,动态查看进程t ...
- 深入理解JVM虚拟机11:Java内存异常原理与实践
本文转自互联网,侵删 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutori ...
- windows下数据库备份bat
@echo offset "Ymd=%date:~,4%%date:~5,2%%date:~8,2%"C:/mysql/bin/mysqldump --opt -u root -- ...
- VUE Right-hand side of ‘instanceof’ is not an object 解决方案
这里要注意一下, props之前没注意写成了 props: { wrd: '', sname:'zs' }, 这样是不能被解析成object的,所以一定要写的更具体一点 ...