题目描述

WW 公司有 mm 个仓库和 nn 个零售商店。第 ii 个仓库有 a_iai​ 个单位的货物;第 jj 个零售商店需要 b_jbj​ 个单位的货物。

货物供需平衡,即\sum\limits_{i=1}^{m}a_i=\sum\limits_{j=1}^{n}b_ji=1∑m​ai​=j=1∑n​bj​。

从第 ii 个仓库运送每单位货物到第 jj 个零售商店的费用为 c_{ij}cij​​​ 。

试设计一个将仓库中所有货物运送到零售商店的运输方案,使总运输费用最少。

输入输出格式

输入格式:

第 11 行有 22 个正整数 mm 和 nn,分别表示仓库数和零售商店数。

接下来的一行中有 mm 个正整数 a_iai​,表示第 ii 个仓库有 a_iai​个单位的货物。

再接下来的一行中有 nn 个正整数 b_jbj​,表示第 jj 个零售商店需要 b_jbj​ 个单位的货物。

接下来的 mm 行,每行有 nn 个整数,表示从第 ii 个仓库运送每单位货物到第 jj 个零售商店的费用 c_{ij}cij​。

输出格式:

两行分别输出最小运输费用和最大运输费用。

输入输出样例

输入样例#1: 复制

2 3
220 280
170 120 210
77 39 105
150 186 122
输出样例#1: 复制

48500
69140 这个题目特别简单,就是一个裸题,不过我的写法复杂了一点。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 2e5 + ;
struct edge
{
int u, v, c, f, cost;
edge(int u, int v, int c, int f, int cost) :u(u), v(v), c(c), f(f), cost(cost) {}
};
vector<edge>e;
vector<int>G[maxn];
int a[maxn];//找增广路每个点的水流量
int p[maxn];//每次找增广路反向记录路径
int d[maxn];//SPFA算法的最短路
int inq[maxn];//SPFA算法是否在队列中
int s, t, exa[maxn];
void init()
{
for (int i = ; i <= maxn; i++)G[i].clear();
e.clear();
}
void add(int u, int v, int c, int cost)
{
e.push_back(edge(u, v, c, , cost));
e.push_back(edge(v, u, , , -cost));
//printf("%d %d %d %d\n", u, v, c, cost);
int m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
bool bellman(int s, int t, int& flow, ll &cost)
{
memset(d, 0xef, sizeof(d));
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ;//源点s的距离设为0,标记入队
p[s] = ; a[s] = INF;//源点流量为INF(和之前的最大流算法是一样的) queue<int>q;//Bellman算法和增广路算法同步进行,沿着最短路拓展增广路,得出的解一定是最小费用最大流
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
inq[u] = ;//入队列标记删除
for (int i = ; i < G[u].size(); i++)
{
edge & now = e[G[u][i]];
int v = now.v;
if (now.c > now.f && d[v] < d[u] + now.cost)
//now.c > now.f表示这条路还未流满(和最大流一样)
//d[v] > d[u] + e.cost Bellman 算法中边的松弛
{
// printf("d[%d]=%d d[%d]=%d %d d[%d]=%d\n", v,d[v],u, d[u], now.cost,v,d[u]+now.cost);
// printf("%d %d %d %d %d %d\n", u, now.u, now.v, now.c, now.f, now.cost);
d[v] = d[u] + now.cost;//Bellman 算法边的松弛
p[v] = G[u][i];//反向记录边的编号
a[v] = min(a[u], now.c - now.f);//到达v点的水量取决于边剩余的容量和u点的水量
if (!inq[v]) { q.push(v); inq[v] = ; }//Bellman 算法入队
}
}
}
// printf("a=%d d=%d\n", a[t], d[t]);
if (d[t] < )return false;//找不到增广路
flow += a[t];//最大流的值,此函数引用flow这个值,最后可以直接求出flow
cost += 1ll * d[t] * 1ll * a[t];//距离乘上到达汇点的流量就是费用
// printf("cost=%lld\n", cost);
for (int u = t; u != s; u = e[p[u]].u)//逆向存边
{
e[p[u]].f += a[t];//正向边加上流量
e[p[u] ^ ].f -= a[t];//反向边减去流量 (和增广路算法一样)
}
return true;
}
int Maxflow(int s, int t, ll & cost)
{
memset(p, , sizeof(p));
cost = ;
int flow = ;
while (bellman(s, t, flow, cost));//由于Bellman函数用的是引用,所以只要一直调用就可以求出flow和cost
return flow;//返回最大流,cost引用可以直接返回最小费用
} bool bellman1(int s, int t, int& flow, long long & cost)
{
memset(d, inf, sizeof(d));
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ;//源点s的距离设为0,标记入队
p[s] = ; a[s] = INF;//源点流量为INF(和之前的最大流算法是一样的) queue<int>q;//Bellman算法和增广路算法同步进行,沿着最短路拓展增广路,得出的解一定是最小费用最大流
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
inq[u] = ;//入队列标记删除
for (int i = ; i < G[u].size(); i++)
{
edge & now = e[G[u][i]];
int v = now.v;
if (now.c > now.f && d[v] > d[u] + now.cost)
//now.c > now.f表示这条路还未流满(和最大流一样)
//d[v] > d[u] + e.cost Bellman 算法中边的松弛
{
d[v] = d[u] + now.cost;//Bellman 算法边的松弛
p[v] = G[u][i];//反向记录边的编号
a[v] = min(a[u], now.c - now.f);//到达v点的水量取决于边剩余的容量和u点的水量
if (!inq[v]) { q.push(v); inq[v] = ; }//Bellman 算法入队
}
}
}
if (d[t] == INF)return false;//找不到增广路
flow += a[t];//最大流的值,此函数引用flow这个值,最后可以直接求出flow
cost += (long long)d[t] * (long long)a[t];//距离乘上到达汇点的流量就是费用
for (int u = t; u != s; u = e[p[u]].u)//逆向存边
{
e[p[u]].f += a[t];//正向边加上流量
e[p[u] ^ ].f -= a[t];//反向边减去流量 (和增广路算法一样)
}
return true;
}
int Minflow(int s, int t, long long & cost)
{
memset(p, , sizeof(p));
cost = ;
int flow = ;
while (bellman1(s, t, flow, cost));//由于Bellman函数用的是引用,所以只要一直调用就可以求出flow和cost
return flow;//返回最大流,cost引用可以直接返回最小费用
}
int qa[], qb[];
int qc[][];
int main()
{
int n, m;
cin >> n >> m;
s = , t = n + m + ;
for (int i = ; i <= n; i++)
{
cin >> qa[i];
add(s, i, qa[i], );
}
for (int i = ; i <= m; i++)
{
cin >> qb[i];
add(i + n, t, qb[i], );
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
cin >> qc[i][j];
add(i, j + n, inf, qc[i][j]);
}
}
ll cost = ;
int ans = Minflow(s, t, cost);
printf("%lld\n", cost);
init();
for (int i = ; i <= n; i++) add(s, i, qa[i], );
for (int i = ; i <= m; i++) add(i + n, t, qb[i], );
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
add(i, j + n, inf, qc[i][j]);
}
cost = ;
ans = Maxflow(s, t, cost);
printf("%lld\n", cost);
return ;
}

P4015 运输问题 网络流问题的更多相关文章

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

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

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

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

  3. Luogu P4015 运输问题

    题目链接 \(Click\) \(Here\) 继续颓网络流\(hhhhh\),虽然这次写的是个大水题,但是早上水一个网络流果然还是让人心情舒畅啊- 最大费用最大流不用非得反着费用建边.只要没有正环, ...

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

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

  5. [洛谷P4015]运输问题

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

  6. P4015 运输问题

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

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

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

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

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

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

随机推荐

  1. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限

    开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...

  2. 网络I/O 工作机制

    数据从一台主机发送到网络中的另一台主机需要经过很多步骤,先得有相互沟通的意向,然后得有物理渠道(物理链路),其次双方还得有语言能够交流,且步调要一致. TCP状态转化 如图,是TCP/IP 的握手过程 ...

  3. App瘦身、性能优化总结

    App瘦身 资源瘦身 使用tinypng压缩PNG图片.视频可以通过 Final cut等软件进行分辨率压缩.音频则降低码率即可. 非必须资源文件可以放到自己服务器上 启动图使用 LaunchScre ...

  4. Linux命令及架构部署大全

    1.Linux系统基础知识 Linux 基础优化配置 Linux系统根目录结构介绍 linux系统重要子目录介绍 Linux基础命令(之一)详解 Linux基础命令(之二)详解 Linux文件系统 L ...

  5. 【TensorFlow篇】--Tensorflow框架实现SoftMax模型识别手写数字集

    一.前述 本文讲述用Tensorflow框架实现SoftMax模型识别手写数字集,来实现多分类. 同时对模型的保存和恢复做下示例. 二.具体原理 代码一:实现代码 #!/usr/bin/python ...

  6. 浅谈RabbitMQ Management

    在上一篇博文(centos安装MQ)中,介绍了如何在linux安装rabbitmq,以及安装维护插件,这篇主要介绍介绍rabbitmq_management的UI. vrabbitmq_managem ...

  7. Springboot整合Elastic-Job(二)

    上文我们讲到Springboot整合Elastic-Job整合的demo,只是简单的实现了主要功能.本文在上文基础上,进行新的调整. 事件追踪 Elastic-Job提供了事件追踪功能,可通过事件订阅 ...

  8. Angular CLI 安装和使用

    1.背景介绍 关于Angular版本,Angular官方已经统一命名Angular 1.x同一为Angular JS:Angular 2.x及以上统称Angular: CLI是Command Line ...

  9. Asp.Net Core 轻松学-一行代码搞定文件上传

    前言     在 Web 应用程序开发过程中,总是无法避免涉及到文件上传,这次我们来聊一聊怎么去实现一个简单方便可复用文件上传功能:通过创建自定义绑定模型来实现文件上传. 1. 实现自定义绑定模型 1 ...

  10. [特别公告]RDIFramework.NET微信公众号迁移通知

    亲爱的伙伴们: 非常感谢您们一直以来对RDIFramework.NET开发框架的关注和支持! 为了进一步完善各项功能,能给大家提供更专业.更官方准确的框架资讯,提供更优质的框架合作服务,我们的微信公众 ...