我明白了,

余量网络 名如其名

比如你f/c=3/5

那么正边2,reverse edge3,加起来是5

在这个你建的新图上找s到t的路径

然后path的最小边权叫delta

给流图的对应path的每条边e都加流 delta,或者 反边减delta (反边的情况)

得到新的流,重复

直到余量网络没有s到t的路径

(最大流-最小割定理也行)

求最小费用流

找个最大流,

然后

建立个新图

比如这条边的性质 容量,代价= 6,3,跑着2的流

那么正向边标4,3

反向标 2,-3

新图上找 负环(负环的代价和为负)

然后min{a_1}

流加上这个

流调整了

重复

直到新图上找不到 负环(负环的代价和为负)

不断调整f得到最大流

我吐了都,要想手撕个算法首先还得理解一些定义

我找了个课件边看边学

https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/07NetworkFlowI.pdf

定义大概就这些

Def. The bottleneck capacity of an augmenting path P is the minimum residual capacity of any edge in P

伪代码

其中,AUGMENT()如下

自己做slide里的quiz





搬运别人的代码


// Copyright srcmake 2018.
// C++ Example Ford Fulkerson Algorithm /* Ford Fulkerson Algorithm:
// 0. Initialize an adjacency matrix to represent our graph.
// 1. Create the residual graph. (Same as the original graph.)
// 2. Create an default parent vector for BFS to store the augmenting path.
// 3. Keep calling BFS to check for an augmenting path (from the source to the sink...
// 4. Find the max flow through the path we found.
// 5. Update the residual capacities of the edges and reverse edges.
// 6. Add this path's flow to our total max flow so far.
*/ // The example graph: https://www.srcmake.com/uploads/5/3/9/0/5390645/maxflow_1_orig.jpg #include <iostream>
#include <vector>
#include <queue> using namespace std; //////////////////////////////////////////////////////////////////////////////////////////////
// See the picture here: https://www.srcmake.com/uploads/5/3/9/0/5390645/adjmatrix_1_orig.jpg
vector< vector<int> > FormAdjMatrix()
{
// Our adjacency list.
vector< vector<int> > adjMatrix; const int n = 6; // Initialize our matrix to all 0s.
for(int i = 0; i < n; i++)
{
vector<int> row;
adjMatrix.push_back(row);
for(int j = 0; j < n; j++)
{
adjMatrix[i].push_back(0);
}
} // First number is the vertex, second is the edge capacity.
adjMatrix[0][1] = 15;
adjMatrix[0][2] = 12;
adjMatrix[1][2] = 9;
adjMatrix[1][3] = 11;
adjMatrix[2][1] = 5;
adjMatrix[2][4] = 13;
adjMatrix[3][2] = 9;
adjMatrix[3][5] = 25;
adjMatrix[4][3] = 8;
adjMatrix[4][5] = 6; // Our graph is now represented as an adjacency list.
return adjMatrix;
}
////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////
// A special BFS version that returns true if there's a path from source to sink.
bool BFS(vector< vector<int> > &resAdjMatrix, int &source, int &sink, vector<int> &parent)
{
// Create an array for all nodes we visited. Initialized to false.
int n = resAdjMatrix.size();
bool visited[n] = { false }; // Create a queue to check each node.
queue<int> q; // Push our source into the queue and mark it as visited. It has no parent.
q.push(source);
visited[source] = true;
parent[source] = -1; // Keep visiting vertices.
while(q.empty() == false)
{
int u = q.front();
q.pop(); // Check all of u's friends.
for(int i = 0; i < n; i++)
{
int v = i;
int capacity = resAdjMatrix[u][v]; // We find a neighbor that hasn't been visited, and the capacity is bigger than 0.
if(visited[v] == false && capacity > 0)
{
// Push the neighbor onto the queue, mark it's parent, and mark it as visited.
q.push(v);
parent[v] = u;
visited[v] = true;
}
}
} // If the sink got visited, then we found a path to it.
if(visited[sink] == true)
{ return true; } return false;
}
////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////
// Use the Ford Fulkerson algorithm. Return the max flow.
int FordFulkerson(vector< vector<int> > &adjMatrix, int &source, int &sink)
{
int maxflow = 0; // 1. Create the residual graph. (Same as the original graph.)
vector< vector<int> > resAdjMatrix;
int n = adjMatrix.size();
for(int i = 0; i < n; i++)
{
vector<int> row;
resAdjMatrix.push_back(row);
for(int j = 0; j < adjMatrix[i].size(); j++)
{
resAdjMatrix[i].push_back(adjMatrix[i][j]);
}
} // 2. Create an empty parent array for BFS to store the augmenting path.
vector<int> parent;
for(int i = 0; i < n; i++)
{
parent.push_back(-1);
} // 3. Keep calling BFS to check for an augmenting path (from the source to the sink...
while(BFS(resAdjMatrix, source, sink, parent) == true)
{
// 4. Find the max flow through the path we just found.
int pathflow = 10000007; // Go through the path we just found. Iterate through the path.
int v = sink;
while(v != source)
{
int u = parent[v]; // The parent. // Update the pathflow to this capacity if it's smaller
int capacity = resAdjMatrix[u][v];
pathflow = min(pathflow, capacity); // Setup for the next edge in the path.
v = u;
} // 5. Update the residual capacities of the edges and reverse edges.
v = sink;
while(v != source)
{
int u = parent[v]; // The parent. // Update the capacities. resAdjMatrix[u][v] -= pathflow;
resAdjMatrix[v][u] += pathflow; // Setup for the next edge in the path.
v = u;
} // 6. Add this path's flow to our total max flow so far.
maxflow += pathflow;
} return maxflow;
}
////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
cout << "Program started.\n"; // Create our adjacency list.
vector< vector<int> > adjMatrix = FormAdjMatrix(); // Call FordFulkerson to get the max flow from the source to the sink.
int source = 0;
int sink = 6; for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 6; j++)
{
int source = i;
int sink = j; if(i == j) { continue; } cout << "The max flow from " << source << " to " << sink << " is: ";
cout << FordFulkerson(adjMatrix, source, sink) << endl;
}
cout << endl;
} cout << "Program ended.\n"; return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////

手撕Ford-Fulkerson algorithm 学一半的笔记的更多相关文章

  1. ACM/ICPC 之 网络流入门-Ford Fulkerson与SAP算法(POJ1149-POJ1273)

    第一题:按顾客访问猪圈的顺序依次构图(顾客为结点),汇点->第一个顾客->第二个顾客->...->汇点 //第一道网络流 //Ford-Fulkerson //Time:47M ...

  2. NN入门,手把手教你用Numpy手撕NN(一)

    前言 这是一篇包含极少数学推导的NN入门文章 大概从今年4月份起就想着学一学NN,但是无奈平时时间不多,而且空闲时间都拿去做比赛或是看动漫去了,所以一拖再拖,直到这8月份才正式开始NN的学习. 这篇文 ...

  3. NN入门,手把手教你用Numpy手撕NN(2)

    这是一篇包含较少数学推导的NN入门文章 上篇文章中简单介绍了如何手撕一个NN,但其中仍有可以改进的地方,将在这篇文章中进行完善. 误差反向传播 之前的NN计算梯度是利用数值微分法,虽容易实现,但是计算 ...

  4. NN入门,手把手教你用Numpy手撕NN(三)

    NN入门,手把手教你用Numpy手撕NN(3) 这是一篇包含极少数学的CNN入门文章 上篇文章中简单介绍了NN的反向传播,并利用反向传播实现了一个简单的NN,在这篇文章中将介绍一下CNN. CNN C ...

  5. 编译原理--05 用C++手撕PL/0

    前言 目录 01 文法和语言.词法分析复习 02 自顶向下.自底向上的LR分析复习 03 语法制导翻译和中间代码生成复习 04 符号表.运行时存储组织和代码优化复习 05 用C++手撕PL/0 在之前 ...

  6. 优雅手撕bind函数(面试官常问)

    优雅手撕bind函数 前言: 为什么面试官总爱让实现一个bind函数? 他想从bind中知道些什么? 一个小小的bind里面内有玄机? 今天来刨析一下实现一个bind要懂多少相关知识点,也方便我们将零 ...

  7. 手撕spring核心源码,彻底搞懂spring流程

    引子 十几年前,刚工作不久的程序员还能过着很轻松的日子.记得那时候公司里有些开发和测试的女孩子,经常有问题解决不了的,不管什么领域的问题找到我,我都能帮她们解决.但是那时候我没有主动学习技术的意识,只 ...

  8. Netty实现高性能IOT服务器(Groza)之手撕MQTT协议篇上

    前言 诞生及优势 MQTT由Andy Stanford-Clark(IBM)和Arlen Nipper(Eurotech,现为Cirrus Link)于1999年开发,用于监测穿越沙漠的石油管道.目标 ...

  9. 手撕RPC框架

    手撕RPC 使用Netty+Zookeeper+Spring实现简易的RPC框架.阅读本文需要有一些Netty使用基础. 服务信息在网络传输,需要讲服务类进行序列化,服务端使用Spring作为容器.服 ...

  10. 手撕公司SSO登陆原理

    Single Sign-on SSO是老生常谈的话题了,但部分同学对SSO可能掌握的也是云里雾里,一知半解.本次手撕公司的SSO登陆原理,试图以一种简单,流畅的形式为你提供 有用的SSO登陆原理. 按 ...

随机推荐

  1. mac常用命令和Git创建tag命令

    一.mac命令 p.p1 { margin: 0; font: 12px ".PingFang SC" } p.p2 { margin: 0; font: 12px "H ...

  2. Peer Review

    What are Peng (Bob) Chi's top 3 strengths?  Can you give an example of how one or two of those stren ...

  3. 群晖Video Station不支持部分视频的解释

    网络上都是替换ffmpeg插件的做法,无非就是替换了3个文件,然后再对其中一个文件进行修改. 然而在DSM7.0.1+VS3.0.2中,这个方法根本无用,最好的结果是之前无法播放的视频播放起来转圈圈而 ...

  4. iOS OC开发,文件的下载、预览

    /// 下载/打开 - (void)downloadActionWithDownloadString:(NSString *)downloadString{ //url : 下载地址 NSString ...

  5. 流量运营(pvuv跳出率漏斗abtest)

    AARRR模型 1. 激活: 拉新,对用户来源渠道进行分析,哪些合作.投放渠道对我们产品更合适 2. 注册: 流量激活之后,如果用户只是点进来就走了,也没什么用,合适的产品功能切合用户需求,才会有转化 ...

  6. Vscode,php运行

    1.下载好vscode,点击左侧扩展,然后搜索php,安装插件 2.打开小皮面板创建网站 点击确认 创建成功 3.浏览器输入http://myblog验证 4.在vscode打开新建的myblog文件 ...

  7. nuxt防止第三方ui多次打包

    在nuxt.config.js中的 build:{ extend(){}, verdor:['element-ui'] }

  8. #Python #字符画 #灰度图 使用Python绘制字符画及其原理

    由于最近身体状况不太好所以更新会有点慢,请大家多多包涵.同时也提醒大家注意保重身体! 前提:默认大家已经正确安装了 Python,且正确将Python配置到了系统Path . 目录 1.字符画的概况 ...

  9. 虚拟机安装windows 7 32位 + sqlserver 2000

    安装包网盘地址:(https://pan.baidu.com/s/1ZoC-cTafBi8zZbCkvvmvNA?pwd=x1y2 提取码:x1y2 ) VMware 安装win7 32 位 http ...

  10. Centos7部署PXE+Kickstart 实现批量安装操作系统

    1.PXE环境概述 作为一名运维人员,在一些中小公司经常会遇到一些机械式的重复工作,比如:批量一次大批量的进行操作系统的安装等等.为了实现自动化运维,减少人员负担我们可以部署以下服务:Kickstar ...