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. mysql 子句、子查询、连接查询

    一.mysql查询的五种子句 where子句(条件查询):按照“条件表达式”指定的条件进行查询. group by子句(分组):按照“属性名”指定的字段进行分组.group by子句通常和count( ...

  2. ps 指令

    ps显示系统当前进程信息, ps 存在多个版本,因此 ps options 的种类繁多.这里只列举平时开发过程中常用的命令,如果有错误或者更好的例子.烦请在评论区指出 语法 ps [options] ...

  3. 题解-AtCoder ARC-078F Mole and Abandoned Mine

    problem ATC-arc078F 题意概要:给定一个 \(n\) 点 \(m\) 边简单无向图(无自环无重边),边有费用,现切去若干条边,使得从 \(1\) 到 \(n\) 有且仅有一条简单路径 ...

  4. git 修改注释

    原文:https://www.jianshu.com/p/098d85a58bf1 修改最后一条注释: git commit --amend 如果已经推送到远程,强制push到远程仓库: git pu ...

  5. VMWare安装Ubuntu16.04

    一 概述 VMware Workstation 12的安装(略过,自行百度) Ubuntu16.04的安装 克隆出多个镜像 二 Ubuntu16.04的安装 1 准备 Window10 专业版(关闭H ...

  6. C# 不是序列化xml 转实体Model【原家独创】

    public static T XmlConvertModel<T>(string xmlStr) where T : class, new()        {            T ...

  7. BeautifulSoup库的安装与使用

    BeautifulSoup库的安装 Win平台:“以管理员身份运行” cmd 执行 pip install beautifulsoup4 演示HTML页面地址:http://python123.io/ ...

  8. Oracle数据库账户口令复杂度-等保测评之身份鉴别

    1.     默认情况下数据库没有启用密码验证函数功能,可通过下面sql查询 SQL> select limit from dba_profiles where RESOURCE_NAME='P ...

  9. Linux安全:Linux如何防止木马

    (一)解答战略 去企业面试时是有多位竞争者的,因此要注意答题的维度和高度,一定要直接秒杀竞争者,搞定高薪offer. (二)解答战术 因为Linux下的木马常常是恶意者通过Web的上传目录的方式来上传 ...

  10. 四、DDL常见操作汇总

    DDL: Data Define Language 数据定义语言,主要用来对数据库.表进行一些管理操作.如:建库.删库.建表.修改表.删除表.对列的增删改等. 一.库的管理 1.创建库 create ...