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 ...
随机推荐
- JS数组方法的的返回值和是否改变该数组总结
concat() 方法 concat() 方法用于连接两个或多个数组. 该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本. 返回值 返回一个新的数组.该数组是通过把所有 arrayX 参数添 ...
- Git+Jenkins配置
一.新建任务 admin账户登陆jenkins,点击新建按钮-> 选择构建一个自由风格的软件项目,item name 随便输入一个自己认为有意义的名字,点击OK 二.源码管理 源码管理-> ...
- 【MyBatis】----【MyBatis】--封装---别名---properties
一.核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration ...
- 【js】什么是函数节流与函数去抖
函数节流 意思:节省流量,不会一直访问. | 指定时间内不执行,指定时间后执行. | 一段时间内只执行一次 场景: 比如控制游戏人物攻击,时间内就算按得很快,也只能砍一刀,过后才能砍第二刀. 搜索引擎 ...
- Android - Retrofit 2.0 使用教程(含实例讲解)
链接:https://blog.csdn.net/carson_ho/article/details/73732076
- JSON.toJSONString时保留null值
QuoteFieldNames———-输出key时是否使用双引号,默认为true WriteMapNullValue——–是否输出值为null的字段,默认为false WriteNullNumberA ...
- 第四周预习作业and第五周作业
第四周预习作业 统计一行文本的单词个数 本题目要求编写程序统计一行字符中单词的个数.所谓"单词"是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个. 输入格式: 输入 ...
- [19/10/14-星期一] Python中的对象和类
一.面向对象 ## 什么是对象? - 对象是内存中专门用来存储数据的一块区域. - 对象中可以存放各种数据(比如:数字.布尔值.代码) - 对象由三部分组成: 1.对象的标识(id) 2.对象的类型( ...
- [19/10/13-星期日] Python中的函数
一.函数 # 第五章 函数 ## 函数简介(function) - 函数也是一个对象 - 对象是内存中专门用来存储数据的一块区域 - 函数可以用来保存一些可执行的代码,并且可以在需要时,对这些语句进行 ...
- [19/06/06-星期四] CSS基础_盒子模型
一.盒子模型(框模型.盒模型) CSS处理网页时,它认为每个元素都在一个不可见的矩形盒子里. 为什么想象成盒子模型?因为把所有元素想象成盒子,那么我们对网页的布局就相当于摆放盒子.我们只需要把相应的盒 ...