P4015 运输问题 最大/最小费用最大流
#include <bits/stdc++.h>
using namespace std;
const int maxn = , inf = 0x3f3f3f3f;
struct Edge {
int from, to, cap, flow, cost;
};
struct MCMF {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn]; void init(int n) {
this->n = n;
for (int i = ; 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, , cost});
edges.push_back((Edge){to, from, , , -cost});
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool BellmanFord(int s, int t, int& flow, int& cost) {
for (int i = ; i <= n; ++i) d[i] = inf;
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = inf; queue<int> que;
que.push(s);
while (!que.empty()) {
int u = que.front(); que.pop();
inq[u] = ;
for (int i = ; i < G[u].size(); ++i) {
Edge& e = edges[G[u][i]];
if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap-e.flow);
if (!inq[e.to]) { que.push(e.to); inq[e.to] = ; }
}
}
}
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]^].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
int mincost(int s, int t) {
int flow = , cost = ;
while (BellmanFord(s, t, flow, cost));
return cost;
}
}mcmf;
int a[maxn], b[maxn], c[maxn][maxn];
int main() {
int m, n; scanf("%d%d",&m,&n);
int s = m+n+, t = m+n+; for (int i = ; i <= m; ++i) scanf("%d",&a[i]);
for (int j = ; j <= n; ++j) scanf("%d",&b[j]);
for (int i = ; i <= m; ++i) {
for (int j = ; j <= n; ++j) {
scanf("%d",&c[i][j]);
}
}
// 最小费用最大流
mcmf.init(n+m+);
for (int i = ; i <= m; ++i) {
mcmf.AddEdge(s,i,a[i],);
}
for (int i = ; i <= m; ++i) {
for (int j = ; j <= n; ++j) {
mcmf.AddEdge(i,j+m,inf,c[i][j]);
}
}
for (int j = ; j <= n; ++j) {
mcmf.AddEdge(j+m,t,b[j],);
}
printf("%d\n",mcmf.mincost(s,t)); // 最大费用最大流
mcmf.init(n+m+);
for (int i = ; i <= m; ++i) {
mcmf.AddEdge(s,i,a[i],);
}
for (int i = ; i <= m; ++i) {
for (int j = ; j <= n; ++j) {
mcmf.AddEdge(i,j+m,inf,-c[i][j]);
}
}
for (int j = ; j <= n; ++j) {
mcmf.AddEdge(j+m,t,b[j],);
}
printf("%d\n",-mcmf.mincost(s,t));
return ;
}
P4015 运输问题 最大/最小费用最大流的更多相关文章
- 洛谷 P4015 运输问题 【最小费用最大流+最大费用最大流】
s向仓库i连ins(s,i,a[i],0),商店向t连ins(i+m,t,b[i],0),商店和仓库之间连ins(i,j+m,inf,c[i][j]).建两次图分别跑最小费用最大流和最大费用最大流即可 ...
- [CODEVS1914] 运输问题(最小费用最大流)
传送门 水题. 建图都不想说了 ——代码 #include <queue> #include <cstdio> #include <cstring> #includ ...
- Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流)
Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流) Description W 公司有m个仓库和n个零售商店.第i个仓库有\(a_i\)个单位的货物:第j个零售商店需要\( ...
- LIbreOJ #6011. 「网络流 24 题」运输问题 最小费用最大流
#6011. 「网络流 24 题」运输问题 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- [板子]最小费用最大流(Dijkstra增广)
最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...
- bzoj1927最小费用最大流
其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→ =_=你TM逗我 刚要删突然感觉dinic的模 ...
- ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)
将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- P3381 【模板】最小费用最大流
P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...
随机推荐
- java中有界队列的饱和策略(reject policy)
文章目录 AbortPolicy DiscardPolicy DiscardOldestPolicy CallerRunsPolicy 使用Semaphore java中有界队列的饱和策略(rejec ...
- KVM虚拟化平台环境部署
一:安装依赖包 二:配置网卡 三:配置环境 实验环境: KVM01 192.168.200.10 关闭防火墙及相关的安全机制 [root@KVM01 ~]# systemctl stop fire ...
- Eureka重点原理解析
前言 带着问题学习,事半功倍.本文将对如下几个问题进行总结说明: 1.EurekaServer端服务注册的流程和设计模式 2.Eureka服务续约的bug 3.EurekaClient的启动流程 4. ...
- 【Linux常见命令】netstat命令
netstat - Print network connections, routing tables, interface statistics, masquerade connections, a ...
- socket编程-多个客户端向服务器发送人脸照片,服务器返回识别结果(服务器使用多线程)...
recognition.py import numpy as np import face_recognition import os class recognition: def __init__( ...
- xml文件错误
2019独角兽企业重金招聘Python工程师标准>>> xml文件错误The processing instruction target matching "[xX][mM ...
- nginx日志、nginx日志切割、静态文件不记录日志和过期时间
2019独角兽企业重金招聘Python工程师标准>>> 12.10 Nginx访问日志 日志格式 vim /usr/local/nginx/conf/nginx.conf //搜索l ...
- MySQL重新初始化安装数据库
删除./mysql/var下的所有数据后,怎么重新安装初始数据库? (1)进入./mysql/bin目录下,执行脚本./mysql_install_db: (2)执行完(1)后,此时会在./mysq ...
- winform练习-通过遍历Control容器中的对象统一委托事件-楼盘选择器
1.窗体布局如下,一个label标签内容如下,一个btnSave按钮,用于保存,其他九个按钮用于选择楼盘. 2. 按钮存于Control容器中,编写方法遍历容器中的button,通过条件过滤掉不是bu ...
- CF思维联系– Codeforces-988C Equal Sums (哈希)
ACM思维题训练集合 You are given k sequences of integers. The length of the i-th sequence equals to ni. You ...