POJ-2516-Minimum Cost(网络流, 最小费用最大流)
链接:
https://vjudge.net/problem/POJ-2516
题意:
Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods (marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport.
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.
思路:
建图, 但是不能对每个商品同时建图,每个商品矩阵分别建图,同时不满足条件就不要跑费用流了..会T.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 50+10;
const int INF = 1e9;
struct Edge
{
int from, to, flow, cap, cost;
Edge(int from, int to, int flow, int cap, int cost)
{
this->from = from;
this->to = to;
this->flow = flow;
this->cap = cap;
this->cost = cost;
}
};
vector<Edge> edges;
vector<int> G[MAXN*MAXN*MAXN];
int Sh[MAXN][MAXN];
int Wo[MAXN][MAXN];
int SumN[MAXN];
int a[MAXN*MAXN];
int Vis[MAXN*MAXN*MAXN], Dis[MAXN*MAXN*MAXN], Pre[MAXN*MAXN*MAXN];
int n, m, k;
int s, t;
void AddEdge(int from, int to, int cap, int cost)
{
edges.push_back(Edge(from, to, 0, cap, cost));
edges.push_back(Edge(to, from, 0, 0, -cost));
int len = edges.size();
G[from].push_back(len-2);
G[to].push_back(len-1);
}
bool SPFA()
{
memset(Dis, MINF, sizeof(Dis));
memset(Vis, 0, sizeof(Vis));
queue<int> que;
Dis[s] = 0;
Vis[s] = 1;
que.push(s);
a[s] = INF;
while (!que.empty())
{
// for (int i = s;i <= t;i++)
// cout << Dis[i] << ' ' ;
// cout << endl;
int u = que.front();
// cout << u << endl;
que.pop();
Vis[u] = 0;
for (int i = 0;i < G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (e.cap > e.flow && Dis[e.to] > Dis[u]+e.cost)
{
Dis[e.to] = Dis[u]+e.cost;
Pre[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap-e.flow);
if (!Vis[e.to])
{
que.push(e.to);
Vis[e.to] = 1;
}
}
}
}
if (Dis[t] != MINF)
return true;
return false;
}
int CostFlow(int &Flow)
{
int cost = 0;
while (SPFA())
{
// cout << 1 << endl;
// int Min = INF;
// for (int i = t;i != s;i = edges[Pre[i]].from)
// Min = min(Min, edges[Pre[i]].cap-edges[Pre[i]].flow);
// cout << Min << endl;
for (int i = t;i != s;i = edges[Pre[i]].from)
{
edges[Pre[i]].flow += a[t];
edges[Pre[i]^1].flow -= a[t];
// Edge &e = edges[Pre[i]], &ee = edges[Pre[i]^1];
// cout << e.from << ' ' << e.to << ' ' << e.flow << ' ' << e.cap << endl;
// cout << ee.from << ' ' << ee.to << ' ' << ee.flow << ' ' << ee.cap << endl;
// cout << endl;
}
cost += a[t]*Dis[t];
Flow += a[t];
}
return cost;
}
void Init()
{
for (int i = 0;i <= n+m+1;i++)
G[i].clear();
edges.clear();
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
while (~scanf("%d %d %d", &n, &m, &k) && (n+m+k))
{
s = 0, t = n+m+1;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= k;j++)
scanf("%d", &Sh[i][j]);
}
memset(SumN, 0, sizeof(SumN));
for (int i = 1;i <= m;i++)
{
for (int j = 1;j <= k;j++)
scanf("%d", &Wo[i][j]), SumN[j] += Wo[i][j];
}
int v;
//shop = n*k
//wo = n*k+m*k
int res = 0, sumflow = 0;
bool ok = true;
for (int i = 1;i <= k;i++)
{
Init();
int tmp = 0;
for (int j = 1;j <= n;j++)
{
AddEdge(s, j, Sh[j][i], 0);
tmp += Sh[j][i];
}
if (tmp > SumN[i])
ok = false;
for (int j = 1;j <= n;j++)
{
for (int z = 1;z <= m;z++)
{
scanf("%d", &v);
AddEdge(j, n+z, INF, v);
}
}
for (int j = 1;j <= m;j++)
AddEdge(n+j, t, Wo[j][i], 0);
if (ok)
res += CostFlow(sumflow);
}
if (!ok)
puts("-1");
else
printf("%d\n", res);
}
return 0;
}
POJ-2516-Minimum Cost(网络流, 最小费用最大流)的更多相关文章
- 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(最小费用最大流)
1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...
- Minimum Cost 【POJ - 2516】【网络流最小费用最大流】
题目链接 题意: 有N个商家它们需要货物源,还有M个货物供应商,N个商家需要K种物品,每种物品都有对应的需求量,M个商家每种物品都是对应的存货,然后再是K个N*M的矩阵表示了K个物品从供货商运送到商家 ...
- POJ2516 Minimum Cost(最小费用最大流)
一开始我把每个店主都拆成k个点,然后建图..然后TLE.. 看题解= =哦,愚钝了,k个商品是独立的,可以分别跑k次最小费用最大流,结果就是k次总和.. #include<cstdio> ...
- POJ2516 Minimum Cost【最小费用最大流】
题意: 有N个客户,M个仓库,和K种货物.已知每个客户需要每种货物的数量,每个仓库存储每种货物的数量,每个仓库运输各种货物去各个客户的单位费用.判断所有的仓库能否满足所有客户的需求,如果可以,求出最少 ...
- Minimum Cost(最小费用最大流,好题)
Minimum Cost http://poj.org/problem?id=2516 Time Limit: 4000MS Memory Limit: 65536K Total Submissi ...
- 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(最小费用流)
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
随机推荐
- RESR API (一)之Requests
Requests 如果您正在做基于REST的Web服务,您应该忽略request.POST. - Malcom Tredinnick,Django开发团队 REST框架的Request类扩展了标准的H ...
- 快速编写 <a> ————CSS3
a{ text-decoration:none; } a:link{ color:white; } a:visited { color:white; } a:hover { color:blue; } ...
- 操作系统汇编语言之AT&T指令
转载时格式有问题,大家看原版吧! 作者:EwenWanW 来源:CSDN 原文:https://blog.csdn.net/xiaoxiaowenqiang/article/details/805 ...
- Our growth depends not on how many experiences we devour, but on how manywe digest.
rot. v/n. 腐烂 vibration.n. 震动 charcoal. n 木炭 wrinkle. v. 长皱纹 geometry. n. 几何学 walnut.n. 核桃 tailor. n. ...
- zip函数用于对列表对应元素打包成元组
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以 ...
- 【Linux开发】【Qt开发】嵌入式Qt程序使用触屏或USB鼠标方式
上文<嵌入式Qt开发-移植到ARM开发板 >介绍了Qt程序的移植,本文再说下如何使开发板Qt程序使用触摸屏或USB方式进行交互. 之前刚把一个qt程序移植到arm板上成功运行显示时就开心的 ...
- Go语言入门篇-高级数据类型
一.数组类型 二.切片类型 切片的更多操作方法 示例: 三.字典类型 四.通道类型 示例: 通道的更多种类 示例: 五.函数 示例: 六.结构体和方法 示例: 七.接口 八.指针 示例: mooc
- Python3 字符编码到底是个什么鬼
首先ASCII码是美国人自己给自己用的,只针对英文及一系列符号,凭想象预留了编码位置,不料有个东方大国文字过于复杂,预留根本不够,所以这个大国重新搞了个编码gb2312.gbk等,结果就是全世界各国都 ...
- vue分别打包测试环境和正式环境
vue打包时使用不同的环境变量 需求 同一个项目通过打包使用不同的环境变量,目前的环境有三个: 一.本地------开发环境 二.线上------测试环境 三.线上------正式环境 我们都知道vu ...
- 【css】子元素浮动到了父元素外,父元素没有随子元素自适应高度,如何解决?
正常情况 如果子元素没有设置浮动(float),父元素的高度会随着子元素高度的改变而改变的. 设置浮动以后 父元素的高度不会随着子元素的高度而变化. 例如:在一个ul中定义若干个li,并设置float ...