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. Shiro授权及注解式开发

    目的: shiro授权 shiro注解式开发 Shiro授权 首先设计shiro权限表: 从图中我们也清晰的看出五张表之间的关系 ShiroUserMapper Set<String> g ...

  2. layer.open自定义弹出位置

    fixed:false,设置top才有效,待测试. 这个设置不起作用 var img = "<img src=\"/_temp/qrcodenet/m/book/book20 ...

  3. 【LeetCode】从排序数组中删除重复项

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...

  4. Dijkstra+Heap模板

    普通Dijkstra: void DijkstraPath(int v0,int vis[],int dist[],int path[]) { int onePath[maxn]; int d; in ...

  5. Java 之 ObjectInputStream 类

    ObjectInputStream 类 1.概述 java.io.ObjectInputStream extends InputStream ObjectInputStream 反序列化流,将之前使用 ...

  6. QSqlDatabase数据库

    #include <QSqlDatabase> #include <QtDebug> #include <QSqlQuery> #include <QSqlE ...

  7. MongoDB的删除操作

    1.MongoDB 删除数据库的语法格式如下:  db.dropDatabase() > show dbs admin .000GB config .000GB local .000GB sda ...

  8. 【postman】postman使用教程

    postman基础功能 一.变量设置 编写的API往往需要在多个环境下执行,而Postman 提供了两种类型的变量:环境变量和全局变量,从而很好的解决了这个问题.同时变量还常用于关联接口间的参数传递. ...

  9. Python 虚拟空间的使用

    使用虚拟环境, 可以将当前项目所使用的依赖与电脑中其他 Python 项目的依赖区分开, 避免依赖版本不匹配带来的问题, 同时也可以防止项目依赖被不当更新. mkdir myproject cd my ...

  10. 你的系统需要SMB2或者更高版本,才能访问共享