题目链接 \(Click\) \(Here\)

继续颓网络流\(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 运输问题的更多相关文章

  1. P4015 运输问题 最大/最小费用最大流

    P4015 运输问题 #include <bits/stdc++.h> using namespace std; , inf = 0x3f3f3f3f; struct Edge { int ...

  2. P4015 运输问题 网络流问题

    题目描述 WW 公司有 mm 个仓库和 nn 个零售商店.第 ii 个仓库有 a_iai​ 个单位的货物:第 jj 个零售商店需要 b_jbj​ 个单位的货物. 货物供需平衡,即\sum\limits ...

  3. 洛谷P4015 运输问题(费用流)

    传送门 源点向仓库连费用$0$,流量为储量的边,商店向汇点连费用$0$,流量为需求的边,然后仓库向商店连流量$inf$,费用对应的边,跑个费用流即可 //minamoto #include<io ...

  4. [洛谷P4015]运输问题

    题目大意:有m个仓库和n个商店.第i个仓库有 $a_{i}$ 货物,第j个商店需要$b_{j}$个货物.从第i个仓库运送每单位货物到第j个商店的费用为$c_{i,j}$​​.求出最小费用和最大费用 题 ...

  5. P4015 运输问题

    \(\color{#0066ff}{题目描述}\) W 公司有 m 个仓库和 n 个零售商店.第 i 个仓库有 \(a_i\) 个单位的货物:第 j 个零售商店需要 \(b_j\) 个单位的货物. 货 ...

  6. 洛谷 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]).建两次图分别跑最小费用最大流和最大费用最大流即可 ...

  7. 洛谷P4015 运输问题(费用流)

    题目描述 WW 公司有 mm 个仓库和 nn 个零售商店.第 ii 个仓库有 a_iai​ 个单位的货物:第 jj 个零售商店需要 b_jbj​ 个单位的货物. 货物供需平衡,即\sum\limits ...

  8. 洛谷P4015 运输问题 网络流24题

    看了下SPFA题解,一个一个太麻烦了,另一个写的很不清楚,而且注释都变成了"????"不知道怎么过的,于是自己来一发SPFA算法. Part 1.题意 M 个仓库,卖给 N 个商店 ...

  9. P4015 运输问题【zkw费用流】

    输入输出样例 输入 #1复制 2 3 220 280 170 120 210 77 39 105 150 186 122 输出 #1复制 48500 69140zuixiaofeiyo 说明/提示 1 ...

随机推荐

  1. 华硕X99-A II 安装使用 志强 XEON E5-1603 v4

    刚开始无法启动,Debug灯的数字不停的轮回变换,后来把XMP开关关闭后,就能正常启动了.如果不行,就多关机几次,一般3次以上应该就可以启动开了.之后就能正常使用了.

  2. NaN与Null与undefiined的关系

    在js中,定义一个变量需要通过关键字var来定义,定义的变量可以是字符串.数字等等都行.但是如果你只是定义了一个变量,没有给他赋值,那么它就默认为'undefined'.例如 var name; co ...

  3. cookie的域,路径

    Cookie 的路径以及 Cookie 域 cookie 路径 cookie 一般都是由于用户访问页面而被创建的,可是并不是只有在创建 cookie 的页面才可以访问这个cookie.在默认情况下,出 ...

  4. Vue过渡状态

    前面的话 Vue 的过渡系统提供了非常多简单的方法设置进入.离开和列表的动效.那么对于数据元素本身的动效呢?包括数字和运算.颜色的显示.SVG 节点的位置.元素的大小和其他的属性等.所有的原始数字都被 ...

  5. poj-2001(字典树)

    题意:给你一堆字符串,我们定义一个字符串可以被缩写成一个字符串(必须是原字符串的前缀),问你每个字符串能辨识的前缀是什么,不能辨识意思是(ab,abc我们缩写成ab): 解题思路:可以用字典树解决,我 ...

  6. Neutron 网络基本概念

    Neutron 网络基本概念 上次我们讨论了 Neutron 提供的功能,今天我们学习 Neutron 模块几个重要的概念. Neutron 管理的网络资源包括 Network,subnet 和 po ...

  7. 微信小程序 canvas 字体自动换行(支持换行符)

    微信小程序 canvas 自动适配 自动换行,保存图片分享到朋友圈  https://github.com/richard1015/News 微信IDE演示代码https://developers.w ...

  8. 趣味网站5个,小鸡词典/中国配色/名著地图/海洋之音/LOGO设计

    一.小鸡词典 很多流行的词语还没有收录到各大词典,却可以在小鸡词典搜索到,小鸡词典是最全的网络流行词语词典. 不少词条搞笑无厘头,撰写词条还会获得红包. 访问地址:https://jikipedia. ...

  9. kubernetes 构架

  10. Django+Xadmin打造在线教育系统(八)

    首页和全局404,500配置 轮播图 公开课 授课机构 新建view ## 首页view class IndexView(View): def get(self,request): # 取出轮播图 a ...