POJ-2516(最小费用最大流+MCMF算法)
Minimum Cost
POJ-2516
- 题意就是有n个商家,有m个供货商,然后有k种商品,题目求的是满足商家的最小花费供货方式。
- 对于每个种类的商品k,建立一个超级源点和一个超级汇点。每个商家和源点连线,容量为需要的商品数,每个供货商和汇点连线,容量为可以提供的商品数。
- 然后对于商家和供货商之间的连线就是,容量为INF,而费用就是题目提供的费用信息。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
const int N=220;
const int INF=0X3F3F3F3F;
int n,m,k;//n表示商家,m表示供应商(0-50),k表示种类(0-3)
int need[N][N];//需求
int provide[N][N];//供应
int volume[N];//表示第k个品的所有的提供数目
struct Edge {
int from, to, cap, flow, cost;
};
struct MCMF {
int n, m;
vector<Edge> edges;
vector<int> G[N];
int d[N], inq[N], p[N], a[N];
void init(int n) {
this->n = n;
for (int i = 0; i <= n; ++i) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap, int cost) {
edges.push_back(Edge{from, to, cap, 0, cost});
edges.push_back(Edge{to, from, 0, 0, -cost});
m = edges.size();
G[from].push_back(m-2); G[to].push_back(m-1);
}
bool spfa(int s, int t, int &flow, int &cost) {
//M(inq, 0); M(d, INF);
memset(inq,0,sizeof(inq));
memset(d,INF,sizeof(d));
d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
queue<int> q;
q.push(s);
while (!q.empty()) {
int x = q.front(); q.pop();
inq[x] = 0;
for (int i = 0; i < G[x].size(); ++i) {
Edge &e = edges[G[x][i]];
if (d[e.to] > d[x] + e.cost && e.cap > e.flow) {
d[e.to] = d[x] + e.cost;
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap-e.flow);
if (inq[e.to]) continue;
q.push(e.to); inq[e.to] = 1;
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
int Mincost(int s, int t) {
int flow = 0, cost = 0;
while (spfa(s, t, flow, cost));
return cost;
}
}solver;
int main(){
while(cin>>n>>m>>k&&(n||m||k)){
memset(volume,0,sizeof(volume));
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
cin>>need[i][j];
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=k;j++){
cin>>provide[i][j];
volume[j]+=provide[i][j];
}
}
bool flag=true;
int cost=0;
for(int s=1;s<=k;s++){
int s1=0,t=n+m+1;
solver.init(n+m+1);
int volume1=0;
for(int i=1;i<=n;i++){
solver.AddEdge(s1,i,need[i][s],0);
volume1+=need[i][s];
}
if(volume1>volume[s])
flag=false;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int co;
cin>>co;
solver.AddEdge(i,n+j,INF,co);
}
}
for(int i=1;i<=m;i++){
solver.AddEdge(i+n,t,provide[i][s],0);
}
if(flag){
cost+=solver.Mincost(s1,t);
}
}
if(flag)
cout<<cost<<endl;
else
{
cout<<-1<<endl;
}
}
return 0;
}
POJ-2516(最小费用最大流+MCMF算法)的更多相关文章
- POJ 2516 最小费用最大流
每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...
- POJ-2195(最小费用最大流+MCMF算法)
Going Home POJ-2195 这题使用的是最小费用流的模板. 建模的时候我的方法出现错误,导致出现WA,根据网上的建图方法没错. 这里的建图方法是每次到相邻点的最大容量为INF,而花费为1, ...
- poj 2195 最小费用最大流模板
/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...
- poj 3422(最小费用最大流)
题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...
- 把人都送到房子里的最小花费--最小费用最大流MCMF
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1533 相邻的容量为inf,费用为1,S到m容量为1,费用为0 ,H到T容量为1,费用为0. 建图跑-最小费 ...
- POJ - 2195 最小费用最大流
题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...
- POJ 2135 最小费用最大流 入门题
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19207 Accepted: 7441 Descri ...
- POJ 2195 - Going Home - [最小费用最大流][MCMF模板]
题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...
- poj 2135最小费用最大流
最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...
随机推荐
- WPF Animation For SizeChanged Of UIElement
效果图 学到一个新词: Show me the money 背景 这几天查资料,看到 CodeProject 上面的一篇 Post <Advanced Custom TreeView Layou ...
- @Indexed 注解
本文转载自:https://www.cnblogs.com/aflyun/p/11992101.html 最近在看 SpringBoot 核编程思想(核心篇),看到走向注解驱动编程这章,里面有讲解到: ...
- HttpClient客户端网络编程——高可用、高并发
本文是HttpClient的学习博客,RestTemplate是基于HttpClient的封装,feign可基于HttpClient进行网络通信. 那么作为较底层的客户端网络编程框架,该怎么配置使其能 ...
- 【非原创】codeforces - 1067A Array Without Local Maximums【dp】
学习博客:戳这里 附本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 co ...
- ajax全局
$.ajaxSetup({ complete: function (xhr) { xhr.promise().done(function (json) { if (json.errorNo == &q ...
- 地址解析协议ARP与逆地址解析协议RARP
IP地址是用来通信的,但是和硬件地址是有区别的.物理地址是数据链路层和物理层使用的地址,IP地址是网络层及以上各层使用的地址. 发送数据时,数据从高层向下层传输,使用IP地址的IP数据报交给下层的数据 ...
- 错误记录:MQJE001: 完成代码为 '2',原因为 '2035'。
在windows server 2008上安装websphere mq7.5 服务端,建立队列.通过java client向我的机器的队列发送消息和接收消息. mq安装成功,队列管理器.队列.通道也都 ...
- React Native & Security
React Native & Security https://reactnative.dev/docs/security React Native blogs https://reactna ...
- windows 10 remote desktop
windows 10 remote desktop https://support.microsoft.com/en-us/help/4028379/windows-10-how-to-use-rem ...
- How to using PyPI publish a Python package
How to using PyPI publish a Python package PyPI & Python package https://pypi.org/ main make a f ...