https://www.luogu.org/problemnew/show/4015

#include <bits/stdc++.h>

#define gc getchar()

using namespace std;
const int N = ;
const int oo = ; int n, m, S, T, now;
int head[N], dis[N], C[N][N], A[N], B[N], pre[N];
bool vis[N * N];
struct Node{int u, v, cost, flow, nxt;} G[(N * N) << ], E[(N * N) << ];
queue <int> Q; inline int read(){
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline void add(int u, int v, int flow, int cost){
G[now].u = u; G[now].v = v; G[now].flow = flow; G[now].cost = cost;
G[now].nxt = head[u]; head[u] = now ++;
} inline void build_G(){
for(int i = ; i <= n; i ++) add(S, i, oo, ), add(i, S, , );
for(int i = ; i <= n; i ++)
for(int j = ; j <= m; j ++)
add(i, n + j, A[i], C[i][j]), add(n + j, i, , - C[i][j]);
for(int i = ; i <= m; i ++) add(n + i, T, B[i], ), add(T, n + i, , );
for(int i = ; i <= now; i ++){
E[i].u = G[i].u; E[i].v = G[i].v; E[i].nxt = G[i].nxt;
E[i].cost = - G[i].cost; E[i].flow = G[i].flow;
}
} inline bool spfa(int start, int endd){
for(int i = S; i <= T; i ++) vis[i] = , dis[i] = oo;
dis[start] = ;
Q.push(start);
while(!Q.empty()){
int topp = Q.front();
Q.pop(); vis[topp] = ;
for(int i = head[topp]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(dis[v] > dis[topp] + G[i].cost && G[i].flow > ){
dis[v] = dis[topp] + G[i].cost;
pre[v] = i;
if(!vis[v]) vis[v] = , Q.push(v);
}
}
}
return dis[endd] != oo;
} inline void Mfmc(int & Cost){
while(spfa(S, T)){
int endd = T, Now;
int min_f = oo;
while() {
Now = pre[endd];
min_f = min(min_f, G[Now].flow);
if(G[Now].u == S) break;
endd = G[Now].u;
}
Cost += dis[T] * min_f;
endd = T;
while(){
Now = pre[endd];
G[Now].flow -= min_f;
G[Now ^ ].flow += min_f;
if(G[Now].u == S) break;
endd = G[Now].u;
}
}
} int main()
{
n = read(); m = read(); T = n + m + ;
for(int i = S; i <= T; i ++) head[i] = -;
for(int i = ; i <= n; i ++) A[i] = read();
for(int i = ; i <= m; i ++) B[i] = read();
for(int i = ; i <= n; i ++) for(int j = ; j <= m; j ++) C[i][j] = read();
build_G();
int Min_cost();
Mfmc(Min_cost);
cout << Min_cost << endl;
for(int i = ; i <= now; i ++){
G[i].u = E[i].u; G[i].v = E[i].v; G[i].nxt = E[i].nxt;
G[i].cost = E[i].cost; G[i].flow = E[i].flow;
}
Min_cost = ;
Mfmc(Min_cost);
cout << - Min_cost;
return ;
}
/*
2 3
220 280
170 120 210
77 39 105
150 186 122
*/

[Luogu] 运输问题 -- 00的更多相关文章

  1. 网络流24题 ——运输问题 luogu 4015

    题目描述:这里 题面已经提示我们这是费用流了 那么由源点向所有仓库连边,容量为仓库原有货物量,费用为0 然后由所有零售商店向汇点连边,容量为一个零售商店的需求量,费用为0 最后由仓库向零售商店连边,容 ...

  2. Luogu P4015 运输问题

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

  3. Luogu 1603 - 斯诺登的密码 - [简单字符串操作]

    题目链接:https://www.luogu.org/problemnew/show/P1603 题目背景 根据斯诺登事件出的一道水题 题目描述 2013年X月X日,俄罗斯办理了斯诺登的护照,于是他混 ...

  4. Luogu 1613 跑路(最短路径,倍增)

    Luogu 1613 跑路(最短路径,倍增) Description 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是 ...

  5. BZOJ 1565 Luogu P2805 [NOI2009]植物大战僵尸 (Tarjan判环、最小割)

    我: "立个flag 14点之前调完这题" 洛谷AC时间: 2019-06-24 14:00:16 实力打脸... 网络流板子从来写不对系列 题目链接: (BZOJ) https: ...

  6. Luogu P2970 [USACO09DEC]自私的放牧

    https://www.luogu.org/problemnew/show/P2970 P2970 [USACO09DEC]自私的放牧 题目描述 Each of Farmer John's N (1 ...

  7. [Luogu 1850] noip16 换教室

    [Luogu 1850] noip16 换教室 好久没有更博客了,先唠嗑一会,花了两天的空闲时间大致做完了昨年的noip真题 虽然在经过思考大部分题目都可出解(天天爱跑步除外),但是并不知道考试时候造 ...

  8. BZOJ 1500 Luogu P2042 [NOI2005] 维护数列 (Splay)

    手动博客搬家: 本文发表于20180825 00:34:49, 原地址https://blog.csdn.net/suncongbo/article/details/82027387 题目链接: (l ...

  9. 【Luogu】P1613 跑路

    [Luogu]P1613 跑路 一.题目 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资 ...

随机推荐

  1. 5-24 c++语言之【基础知识】

    最近一段时间继续开始了c++的学习,作为c plus plus 难免会与c语言做一个对比,很明显的感受到c++语言注重代码的复用性和拓展性,而c语言更加注重其算法的高效性,这也是今后需要注意的地方,避 ...

  2. littleFS在RT1052移植笔记

    环境:rt1052单片机+16G tf卡 为什么使用littleFS? NXP官方SDK支持! 先进的损耗平衡功能(wear leveling)可提供最长的快闪及内存寿命及最大的使用量.因为FALSH ...

  3. 资深程序员推荐必备书籍 《C语言程序设计》

    当下,IT行业发展日趋迅猛,产值成倍增长,高薪的诱惑更是驱使许多人想要进入IT行业发展.为了使大家更全面理解C语言程序设计,由千锋教研院高教产品研发部编著.清华大学出版社倾情出版的<C语言程序设 ...

  4. C# 使用Berkeley DB

    Berkeley DB是一个开源的文件数据库,介于关系数据库与内存数据库之间.简称BDB Berkeley DB是嵌入式键值数据库库,为应用程序提供可扩展的高性能数据管理服务. Berkeley DB ...

  5. WebUploader 上传图片回显

    /* fileMaxCount 最大文件数 buttonText 按钮文本 multiple 是否多选 */ (function ($) { $.fn.extend({ uploadImg: func ...

  6. .netCore 动态织入

    using Microsoft.Extensions.DependencyInjection; using System; using System.Reflection; namespace Aop ...

  7. 将netcore网站部署到docker容器中

    一.背景 最近一直在看docker的教程,基础知识看的差不多了.理论总要运用于实践,所以下面我们就来把最简单的一个netcore网站托管到docker容器中. 环境:1.docker for wind ...

  8. 【OF框架】在Visual Studio中发布Docker镜像,推送镜像到Azure容器注册表

    准备 拥有Azure账号,已经创建 Azure容器注册表,获得注册表地址.账号.密码 本地已经在Visual Studio登录Azure账号. 本地已经拥有Docker环境 注意:首次发布Docker ...

  9. spring--分类索引

    1.过时api Spring(一)解决XmlBeanFactory过时问题 元素 'ref' 中不允许出现属性 'local' Spring学习笔记 关于spring 2.x中dependency-c ...

  10. frp服务搭建

    安装命令 C wget --no-check-certificate https://raw.githubusercontent.com/clangcn/onekey-install-shell/ma ...