P4015 运输问题

 #include <bits/stdc++.h>
using namespace std;
const int maxn = , inf = 0x3f3f3f3f;
struct Edge {
int from, to, cap, flow, cost;
};
struct MCMF {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn]; void init(int n) {
this->n = n;
for (int i = ; i <= n; ++i) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap, int cost) {
edges.push_back((Edge){from, to, cap, , cost});
edges.push_back((Edge){to, from, , , -cost});
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool BellmanFord(int s, int t, int& flow, int& cost) {
for (int i = ; i <= n; ++i) d[i] = inf;
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = inf; queue<int> que;
que.push(s);
while (!que.empty()) {
int u = que.front(); que.pop();
inq[u] = ;
for (int i = ; i < G[u].size(); ++i) {
Edge& e = edges[G[u][i]];
if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap-e.flow);
if (!inq[e.to]) { que.push(e.to); inq[e.to] = ; }
}
}
}
if (d[t] == inf) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
int mincost(int s, int t) {
int flow = , cost = ;
while (BellmanFord(s, t, flow, cost));
return cost;
}
}mcmf;
int a[maxn], b[maxn], c[maxn][maxn];
int main() {
int m, n; scanf("%d%d",&m,&n);
int s = m+n+, t = m+n+; for (int i = ; i <= m; ++i) scanf("%d",&a[i]);
for (int j = ; j <= n; ++j) scanf("%d",&b[j]);
for (int i = ; i <= m; ++i) {
for (int j = ; j <= n; ++j) {
scanf("%d",&c[i][j]);
}
}
// 最小费用最大流
mcmf.init(n+m+);
for (int i = ; i <= m; ++i) {
mcmf.AddEdge(s,i,a[i],);
}
for (int i = ; i <= m; ++i) {
for (int j = ; j <= n; ++j) {
mcmf.AddEdge(i,j+m,inf,c[i][j]);
}
}
for (int j = ; j <= n; ++j) {
mcmf.AddEdge(j+m,t,b[j],);
}
printf("%d\n",mcmf.mincost(s,t)); // 最大费用最大流
mcmf.init(n+m+);
for (int i = ; i <= m; ++i) {
mcmf.AddEdge(s,i,a[i],);
}
for (int i = ; i <= m; ++i) {
for (int j = ; j <= n; ++j) {
mcmf.AddEdge(i,j+m,inf,-c[i][j]);
}
}
for (int j = ; j <= n; ++j) {
mcmf.AddEdge(j+m,t,b[j],);
}
printf("%d\n",-mcmf.mincost(s,t));
return ;
}

P4015 运输问题 最大/最小费用最大流的更多相关文章

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

  2. [CODEVS1914] 运输问题(最小费用最大流)

    传送门 水题. 建图都不想说了 ——代码 #include <queue> #include <cstdio> #include <cstring> #includ ...

  3. Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流)

    Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流) Description W 公司有m个仓库和n个零售商店.第i个仓库有\(a_i\)个单位的货物:第j个零售商店需要\( ...

  4. LIbreOJ #6011. 「网络流 24 题」运输问题 最小费用最大流

    #6011. 「网络流 24 题」运输问题 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 ...

  5. [板子]最小费用最大流(Dijkstra增广)

    最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...

  6. bzoj1927最小费用最大流

    其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→   =_=你TM逗我 刚要删突然感觉dinic的模 ...

  7. ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)

    将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...

  8. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  9. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

随机推荐

  1. Mac安装Nginx、Mysql、PHP、Redis

    安装xcode命令行工具的命令 xcode-select --install   安装homebrew: ruby -e "$(curl -fsSL https://raw.githubus ...

  2. mysql---3种常用引擎 和优点

  3. TIME_WAIT的危害与避免

    time-wait的产生: 在TCP连接中四次挥手关闭连接时,主动关闭连接的一方(上图中时Client)会在发送最后一条ACK报文后维持一段时长2MSL(MSL指的是数据包在网络中的最大生存时间)的等 ...

  4. js 运动函数篇 (一) (匀速运动、缓冲运动、多物体运动、多物体不同值运动、多物体多值运动)层层深入

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         本篇文章为您分析一下原生JS写 匀速运动.缓冲运动.多物体运 ...

  5. IDEA 之 Java项目复制

    1.复制一个项目,并改名字  2.更改以下文件名字  3.将以下文件中的原有名字,替换成更改后的名字(例如MyWebapp07替换成MyWebapp08) 4.将out文件夹给删除 5.然后用IDEA ...

  6. 《Redis设计与实现》之第十二章:事件

    Redis服务器是一个事件驱动程序,服务器需要处理两类事件: 文件事件: 文件事件就是服务器对套接字(socket)操作的抽象,服务器和客户端的通信会产生文件事件 时间事件: 时间事件就是服务器对定时 ...

  7. jQuery学习(三)

    事件 on方法可以将一个事件绑定在jQuery对象上,当你的操作触发了这些事件时,便会调用你所绑定的函数. 例如,给某个超链接绑定点击事件. <head> <meta http-eq ...

  8. echarts自定义tooltip显示

    使用echarts展示图形的时候,鼠标滑倒图像上,想展示除了系列名,数据名,数据值以外的数据,这时需要使用tooltip的fommater方式进行配置,另外对数据格式也有一定的要求. 如图所示:如果想 ...

  9. POJ - 3278 Catch That Cow 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  10. ST函数(ST表)RMQ O(1)查询 离线

    ST算法是基于倍增的动态规划算法. #include<iostream> #include<cstdio> #include<cstdlib> #include&l ...