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算法)的更多相关文章

  1. POJ 2516 最小费用最大流

    每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...

  2. POJ-2195(最小费用最大流+MCMF算法)

    Going Home POJ-2195 这题使用的是最小费用流的模板. 建模的时候我的方法出现错误,导致出现WA,根据网上的建图方法没错. 这里的建图方法是每次到相邻点的最大容量为INF,而花费为1, ...

  3. poj 2195 最小费用最大流模板

    /*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...

  4. poj 3422(最小费用最大流)

    题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...

  5. 把人都送到房子里的最小花费--最小费用最大流MCMF

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=1533 相邻的容量为inf,费用为1,S到m容量为1,费用为0 ,H到T容量为1,费用为0. 建图跑-最小费 ...

  6. POJ - 2195 最小费用最大流

    题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...

  7. POJ 2135 最小费用最大流 入门题

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19207   Accepted: 7441 Descri ...

  8. POJ 2195 - Going Home - [最小费用最大流][MCMF模板]

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...

  9. poj 2135最小费用最大流

    最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...

随机推荐

  1. codeforces632E. Thief in a Shop (dp)

    A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contai ...

  2. DNS 是什么?如何运作的?

    前言 我们在上一篇说到,IP 地址的发明把我们纷乱复杂的网络设备整齐划一地统一在了同一个网络中. 但是类似于 192.168.1.0 这样的地址并不便于人类记忆,于是发明了 域名(Domain Nam ...

  3. c#记两个变量进行值交换

    今天腊月二十九啦,无心上班,专注划水.然后就在那里翻帖子消磨时光. 看到了这样一个问题,有人提问为什么   a=b+(b=a)*0  ??? 第一眼看上去,我也有点蒙,仔细推敲了一下,嗯~的确是交换了 ...

  4. [Python] Pandas 对数据进行查找、替换、筛选、排序、重复值和缺失值处理

    目录 1. 数据文件 2. 读数据 3. 查找数据 4. 替换数据 4.1 一对一替换 4.2 多对一替换 4.3 多对多替换 5. 插入数据 6. 删除数据 6.1 删除列 6.2 删除行 7. 处 ...

  5. leetcode 1 两数之和 hashmap

    主要是hashmap.还有边插入边查找,提高效率和降低空间复杂度. 之前一直用map,结果发现还有hashmap,效率更高. 注意名称空间为 using namespace __gnu_cxx; 问题 ...

  6. 2019牛客多校第六场H Pair(数位DP 多个数相关)题解

    题意: 传送门 给你\(A,B,C\),要求你给出有多少对\((x, y)\)满足\(x\in [1,A],y\in [1,B]\),且满足以下任意一个条件:\(x \& y > C\) ...

  7. 杭电多校HDU 6656 Kejin Player(概率DP)题解

    题意: 最低等级\(level\ 1\),已知在\(level\ i\)操作一次需花费\(a_i\),有概率\(p_i\)升级到\(level\ i+1\),有\(1 - p_i\)掉级到\(x_i( ...

  8. 杭电多校HDU 6599 I Love Palindrome String (回文树)题解

    题意: 定义一个串为\(super\)回文串为: \(\bullet\) 串s为主串str的一个子串,即\(s = str_lstr_{l + 1} \cdots str_r\) \(\bullet\ ...

  9. 二、mycat基础知识、基本配置

    官网 http://www.mycat.io/ Mycat 概要介绍 https://github.com/MyCATApache/Mycat-Server 入门指南 https://github.c ...

  10. five86-1 (OpenNetadmin RCE+cp命令提权+crunch生成密码字典)

    Vulnhub-Five86-1 实验环境 kali攻击机ip:192.168.56.116 Five86-1靶机ip:192.168.56.121 知识点及工具 nmap扫描 john爆破 Open ...