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 ...
随机推荐
- EMQ插件通过HTTP连接认证服务器实现认证
需求 在EMQ中添加认证插件,将到来的MQTT连接的ClientID.UserName.Password通过HTTP协议发送到认证服务器,用返回的数据决定是否允许该连接: 在连接时和断开时向服务器发送 ...
- 阶段3 1.Mybatis_03.自定义Mybatis框架_1.自定义Mybatis的分析-执行查询所有分析
- SELECTION-SCREEN屏幕范例
1. SELECTIION-SCREEN的語法: SELECTION-SCREEN BEGIN OF SCREEN SCR....SELECTION-SCREEN END OF SCREEN SCR. ...
- Java面试题集(86-115)
Java程序员面试题集(86-115) 摘要:下面的内容包括Struts 2和Hibernate的常见面试题,虽然Struts 2在2013年6月曝出高危漏洞后已经显得江河日下,而Spring MVC ...
- 【web前端】前段时间的面题整理(1)
这是我的试题答案整理,可能有多种答案.我也就写了一两种.在慢慢整合中 第一题 用js实现随机选取10-100之间的10个数字,存入一个数组,去重后求和(保证这10个数字不能出现重复) 要求:去重不能使 ...
- 快速编写 <a> ————CSS3
a{ text-decoration:none; } a:link{ color:white; } a:visited { color:white; } a:hover { color:blue; } ...
- House_Of_Spirit ctf oreo程序分析
oreo程序下载 提取码:t4xx 程序分析 int __cdecl main() { leave_add = 0; leave_del = 0; leave_buf = (char *)&u ...
- 链路聚合teaming(网卡绑定技术)2
一.sentos7网卡绑定技术之teaming 这里介绍两种最常见的双网卡绑定模式: (1) roundrobin - 轮询模式 所有链路处于负载均衡状态,这种模式的特点增加了带宽,同时支持容错能力. ...
- IDEA在resources下创建多级目录
在resource下,创建多级目录,应在每个目录之间用"/"隔开,这样就不需要再手动一层层目录的分别添加了!
- C++自学教程第一课——你好世界,我是柠檬鲸。
C++系列教程现在在自己学校的一个博客平台发布,几个朋友一起搭建的 [C++基础教程系列](https://blog.ytmaxoj.org/cpp_basic_liuary-0/) 下面是原来的正文 ...