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. java 线程并发(生产者、消费者模式)

    线程并发协作(生产者/消费者模式) 多线程环境下,我们经常需要多个线程的并发和协作.这个时候,就需要了解一个重要的多线程并发协作模型“生产者/消费者模式”. Ø 什么是生产者? 生产者指的是负责生产数 ...

  2. 远程 Linux(Ubuntu 18)添加字体

    安装 xshell与xftp 连接xshell 点击 xshell上方工具栏中的xftp图标, 自动连接xftp linux下创建字体目录 su cd / cd usr/share/fonts mkd ...

  3. java输出月的日历控制台

    LocalDate date=LocalDate.now(); int month=date.getMonthValue(); int today=date.getDayOfMonth(); date ...

  4. UI5-技术篇-Implementing Expand Entity/Entity Set

    转载:https://blogs.sap.com/2014/07/18/implementing-expand-entityentity-set/ Requirement Considering a ...

  5. c# 抽象工厂设计模式

  6. Python面向对象之进阶

    一.property 内置函数 装饰器的使用:所有的装饰器函数.方法.类的上一行直接@装饰器的名字 装饰器的分类: ① 装饰函数 ② 装饰方法 ③ 装饰类 property 是一个装饰器函数 @pro ...

  7. MySQL处理达到百万级数据时,如何优化?

    1.两种查询引擎查询速度(myIsam 引擎 ) InnoDB 中不保存表的具体行数,也就是说,执行select count(*) from table时,InnoDB要扫描一遍整个表来计算有多少行. ...

  8. java - day014 - 编译期,运行期

    编译期 静态成员 私有变量 成员变量 运行期 非静态方法 package day1401; public class Test1 { public static void main(String[] ...

  9. Python-共享引用

    A会改变么? 下面三小段代码,A的值都会改变么? >>> A = "spam" >>> B = A >>> B = " ...

  10. STM32 HAL库学习系列第8篇---回调函数总结

    普通函数与回调函数的区别:就是ST将中断封装,给使用者的API,就是标准库的中断函数 对普通函数的调用: 调用程序发出对普通函数的调用后,程序执行立即转向被调用函数执行,直到被调用函数执行完毕后,再返 ...