题目链接 \(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. TestNG之使用ReportNG生成测试报告

    TestNG使用ReportNG生成测试报告会更加美观. 依赖包 <!--testNG报告依赖包--> <dependency> <groupId>org.test ...

  2. 一、hadoop部署

    一.Java环境 yum 安装方式安装 1.搜索JDK安装包 yum search java|grep jdk 2.安装 yum install java-1.8.0-openjdk-src.x86_ ...

  3. 二、两条Linux删除数据跑路命令

    一.rm rm -rf / 无提示循环删除根目录,,删除存在被恢复的可能 二.dd dd if=/dev/urandom of=/dev/hda1 随机填写数据到相应分区,直到填满为止.重写后的分区无 ...

  4. python工具使用笔记

    1.pip pip是Python官方推荐的包管理工具,在doc界面直接使用pip或者pip3命令即可,例如安装gensim: C:\Users\kayan.sjc>pip3 install -- ...

  5. 微服务 Micro services

    微服务 (Microservices) 是一种软件架构风格,它是以专注于单一责任与功能的小型功能区块 (Small Building Blocks) 为基础,利用模组化的方式组合出复杂的大型应用程序, ...

  6. Asp.Net Core get client IP

    不废话,直接上代码,你懂得. public string GetRequestIP(bool tryUseXForwardHeader = true) { string ip = null; // t ...

  7. kubernetes增加污点,达到pod是否能在做节点运行

    master node参与工作负载 (只在主节点执行)使用kubeadm初始化的集群,出于安全考虑Pod不会被调度到Master Node上,也就是说Master Node不参与工作负载. 这里搭建的 ...

  8. Scrapy爬取伯乐在线文章

    首先搭建虚拟环境,创建工程 scrapy startproject ArticleSpider cd ArticleSpider scrapy genspider jobbole blog.jobbo ...

  9. A/B HDU - 1576 (exgcd)

    要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input数据的第一行是一个T,表示有T组数据. 每组数据有两 ...

  10. 【CF768G】The Winds of Winter 可持久化线段树 DFS序

    题目大意 给定一棵\(n\)个点的树,对于树上每个结点,将它删去,然后可以将得到的森林中任意一个点与其父亲断开并连接到另一颗树上,对每一个点求出森林中所有树\(size\)最大值的最小值. \(n\l ...