Luogu P4015 运输问题
继续颓网络流\(hhhhh\),虽然这次写的是个大水题,但是早上水一个网络流果然还是让人心情舒畅啊~
最大费用最大流不用非得反着费用建边.只要没有正环,初始化的时候小心一点就好啦~
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int cnt = -1, head[210];
struct edge {
int nxt, to, w, f;
}e[40010];
void add_edge (int from, int to, int flw, int val) {
e[++cnt].nxt = head[from];
e[cnt].to = to;
e[cnt].f = flw;
e[cnt].w = val;
head[from] = cnt;
}
void add_len (int u, int v, int f, int w) {
add_edge (u, v, f, +w);
add_edge (v, u, 0, -w);
}
int _head[210]; edge _e[40010];
int n, m, numA[110], numB[110], cost[110][110];
int A (int x) {return n * 0 + x;}
int B (int x) {return n * 1 + x;}
queue <int> q;
int dis[210], vis[210], flow[210];
int pre_edge[210], pre_node[210];
bool can_use (int u, int v, int i, int type) {
if (type == +1) return dis[v] > dis[u] + e[i].w;
if (type == -1) return dis[v] < dis[u] + e[i].w;
}
int spfa (int s, int t, int type) {
memset (vis, 0, sizeof (vis));
memset (flow, 0x3f, sizeof (flow));
if (type == +1) memset (dis, +0x3f, sizeof (dis));
if (type == -1) memset (dis, -0x3f, sizeof (dis));
vis[s] = true; dis[s] = 0; q.push (s);
while (!q.empty ()) {
int u = q.front (); q.pop ();
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].to;
if (can_use (u, v, i, type) && e[i].f) {
dis[v] = dis[u] + e[i].w;
flow[v] = min (flow[u], e[i].f);
pre_edge[v] = i;
pre_node[v] = u;
if (!vis[v]) {
vis[v] = true;
q.push (v);
}
}
}
vis[u] = false;
}
return flow[t] != INF;
}
int MCMF (int s, int t, int type) {
int cost = 0;
while (spfa (s, t, type)) {
cost += dis[t] * flow[t];
int u = t;
while (u != s) {
e[pre_edge[u] ^ 0].f -= flow[t];
e[pre_edge[u] ^ 1].f += flow[t];
u = pre_node[u];
}
}
return cost;
}
int main () {
memset (head, -1, sizeof (head));
cin >> n >> m;
int s = n + m + 1;
int t = n + m + 2;
for (int i = 1; i <= n; ++i) {cin >> numA[i]; add_len (s, A (i), numA[i], 0);}
for (int i = 1; i <= m; ++i) {cin >> numB[i]; add_len (B (i), t, numB[i], 0);}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> cost[i][j];
add_len (A (i), B (j), INF, cost[i][j]);
}
}
memcpy (_e, e, sizeof (e)); memcpy (_head, head, sizeof (head));
cout << MCMF (s, t, +1) << endl;
memcpy (e, _e, sizeof (e)); memcpy (head, _head, sizeof (head));
cout << MCMF (s, t, -1) << endl;
}
Luogu P4015 运输问题的更多相关文章
- P4015 运输问题 最大/最小费用最大流
P4015 运输问题 #include <bits/stdc++.h> using namespace std; , inf = 0x3f3f3f3f; struct Edge { int ...
- P4015 运输问题 网络流问题
题目描述 WW 公司有 mm 个仓库和 nn 个零售商店.第 ii 个仓库有 a_iai 个单位的货物:第 jj 个零售商店需要 b_jbj 个单位的货物. 货物供需平衡,即\sum\limits ...
- 洛谷P4015 运输问题(费用流)
传送门 源点向仓库连费用$0$,流量为储量的边,商店向汇点连费用$0$,流量为需求的边,然后仓库向商店连流量$inf$,费用对应的边,跑个费用流即可 //minamoto #include<io ...
- [洛谷P4015]运输问题
题目大意:有m个仓库和n个商店.第i个仓库有 $a_{i}$ 货物,第j个商店需要$b_{j}$个货物.从第i个仓库运送每单位货物到第j个商店的费用为$c_{i,j}$.求出最小费用和最大费用 题 ...
- P4015 运输问题
\(\color{#0066ff}{题目描述}\) W 公司有 m 个仓库和 n 个零售商店.第 i 个仓库有 \(a_i\) 个单位的货物:第 j 个零售商店需要 \(b_j\) 个单位的货物. 货 ...
- 洛谷 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]).建两次图分别跑最小费用最大流和最大费用最大流即可 ...
- 洛谷P4015 运输问题(费用流)
题目描述 WW 公司有 mm 个仓库和 nn 个零售商店.第 ii 个仓库有 a_iai 个单位的货物:第 jj 个零售商店需要 b_jbj 个单位的货物. 货物供需平衡,即\sum\limits ...
- 洛谷P4015 运输问题 网络流24题
看了下SPFA题解,一个一个太麻烦了,另一个写的很不清楚,而且注释都变成了"????"不知道怎么过的,于是自己来一发SPFA算法. Part 1.题意 M 个仓库,卖给 N 个商店 ...
- P4015 运输问题【zkw费用流】
输入输出样例 输入 #1复制 2 3 220 280 170 120 210 77 39 105 150 186 122 输出 #1复制 48500 69140zuixiaofeiyo 说明/提示 1 ...
随机推荐
- 四、K8S
一.查看日志 journalctl -xeu kubelet
- mysql group by 对多个字段进行分组
在平时的开发任务中我们经常会用到MYSQL的GROUP BY分组, 用来获取数据表中以分组字段为依据的统计数据.比如有一个学生选课表,表结构如下: Table: Subject_Selection S ...
- 深度学习中dropout策略的理解
现在有空整理一下关于深度学习中怎么加入dropout方法来防止测试过程的过拟合现象. 首先了解一下dropout的实现原理: 这些理论的解释在百度上有很多.... 这里重点记录一下怎么实现这一技术 参 ...
- faster rcnn训练自己的数据集
采用Pascal VOC数据集的组织结构,来构建自己的数据集,这种方法是faster rcnn最便捷的训练方式
- Android 右上角菜单栏
1 创建菜单栏 在res下新建menu文件夹,并且创建righttopmenu.xml righttopmenu.xml: <?xml version="1.0" encod ...
- python+appium里的等待时间
为什么要用等待时间: 今天在写App的自动化的脚本时发现一个元素,时而能点击,时而又不能点击到,很是心塞,原因是:因为元素还没有被加载出来,查找的代码就已经被执行了,自然就找不到元素了.解决方式:可以 ...
- spi slaver接口的fpga实现
前言 spi从机接口程序,数据位8bit,sck空闲时低电平,工作时第一个沿数据传输.只有一个从机,cs低电平片选,slaver开始工作. 流程: 接口定义: 编码实现:(版权所有,请勿用于商业用途, ...
- hdu 6253 (bfs打表)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6253 题意: 马可以往一个方向走两步,然后转个弯走一步,这样算一次动作,求问马n次动作后,能到达多少个点, ...
- jQuery 方式模拟提交表单
//add test moudle define(function(require , exports , module) { //=========== 不使用模块化只使用如下代码即可 start ...
- Python中使用operator模块实现对象的多级排序
Python中使用operator模块实现对象的多级排序 今天碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能 ...