网络流之最大流问题

Reference:

http://blog.csdn.net/rrerre/article/details/6751520

http://blog.csdn.net/y990041769/article/details/21026445

http://www.nocow.cn/index.php/Translate:USACO/NetworkFlow

 

最大流Edmonds_Karp算法模板:

EK算法即增广路算法。

最大流最小割定理:最大流等于最小割

见白书P210

 

算法思想:

step 1. 令所有弧的流量为0,从而构造一个流量为0的可行流f(称作零流)。
step 2. 若f中找不到可改进路则转step 5;否则找到任意一条可改进路P。
step 3. 根据P求delta。
step 4. 以delta为改进量,更新可行流f。转step 2。
step 5. 算法结束。此时的f即为最大流。

算法的关键步骤是step 2,即:判断是否存在可改进路,若存在又如何求。
可以考虑用广度优先搜索。设置标志数组,记录顶点是不是被访问过;使用队列来存储已经访问过的顶点;另用一个一维数组p[i],记录每个顶点是由哪个顶点扩展而来(即记录父亲节点)。
首先S入队列。然后每次取队首顶点v,分析所有与v相邻的未访问顶点u:
1、存在弧<v, u>(正向弧),且u未访问。若f(v,u)<C(v,u)(非饱和弧),那么u入队列,给u打上“已访问”的标志,记u的父亲节点为v。
2、存在弧<u, v>(反向弧),且u未访问。若f(u,v) > 0(非零流弧),那么u入队列,给u打上“已访问”的标志,记u的父亲节点为-v。(以示和正向弧的区别)。
扩展完成后,若T还没有被访问就必然不存在可改进路;否则就从T出发,根据记录好的每个顶点的父亲节点信息,顺藤摸瓜,找出可改进路(同时还可以计算出delta)。

 

起点st,终点m

#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
int n,m,st,en;
int cap[300][300];    //cap[u][v]:边(u,v)上的最大流量
int flow[300][300];    //flow[u][v]:边(u,v)上当前的流量
int a[300];        //a[u]:访问标记,同时还记录下delta值
int p[300];        //记录父节点用
const int inf=100000000;int EK()            //st-->m
{queue<int> Q;memset(flow,0,sizeof(flow));memset(p,-1,sizeof(p));int f=0,minflow=inf;while(1){memset(a,0,sizeof(a));a[st]=inf;          Q.push(st);         while(!Q.empty()){int u=Q.front();Q.pop();for(int v=1;v<=m;v++)if(!a[v]&&cap[u][v]>flow[u][v]){p[v]=u;Q.push(v);a[v]=a[u]<cap[u][v]-flow[u][v]?a[u]:cap[u][v]-flow[u][v];}}if(a[m]==0) break;for (int u=m;u!=st;u=p[u])          {flow[p[u]][u]+=a[m];flow[u][p[u]]-=a[m];}f+=a[m];}return f;
}int main()
{int S,E,C;while (cin>>n>>st>>m)       //st->m
    {memset(cap,0,sizeof(cap));for (int i=1;i<=n;i++){cin>>S>>E>>C;cap[S][E]+=C;    //处理重边。有些题目,一条路上先给了容量30,然后重复了一次50,这时候这条路上的容量应该是30+50。
        }cout<<EK()<<endl;}return 0;
}
View Code

 

模板例题:

hdu1532

 

补充:需要拆点的问题:POJ3281

http://www.2cto.com/kf/201210/164289.html

http://moxi466839201.blog.163.com/blog/static/18003841620112316351118/

 

-------------------------------------------------------------------------------------------

补充个ISAP模板,比EK算法快,但是难想难写。看不懂T^T...

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;
typedef  struct {int v,next,val;} edge;
const int MAXN=20010;
const int MAXM=500010;
edge e[MAXM];
int p[MAXN],eid;
void init(){memset(p,-1,sizeof(p));eid=0;}
void insert1(int from,int to,int val) //有向
{e[eid].v=to;e[eid].val=val;e[eid].next=p[from];p[from]=eid++;swap(from,to);e[eid].v=to;e[eid].val=0;e[eid].next=p[from];p[from]=eid++;
}
void insert2(int from,int to,int val) //无向
{e[eid].v=to;e[eid].val=val;e[eid].next=p[from];p[from]=eid++;swap(from,to);e[eid].v=to;e[eid].val=val;e[eid].next=p[from];p[from]=eid++;
}
int n,m;//n为点数 m为边数
int h[MAXN];
int gap[MAXN];
int s,t;
int dfs(int pos,int cost)
{if (pos==t) return cost;int j,minh=n-1,lv=cost,d;for (j=p[pos];j!=-1;j=e[j].next){int v=e[j].v,val=e[j].val;if(val>0){if (h[v]+1==h[pos]){if (lv<e[j].val) d=lv;else d=e[j].val;d=dfs(v,d);e[j].val-=d;e[j^1].val+=d;lv-=d;if (h[s]>=n) return cost-lv;if (lv==0) break;}if (h[v]<minh)    minh=h[v];}}if (lv==cost){--gap[h[pos]];if (gap[h[pos]]==0) h[s]=n;h[pos]=minh+1;++gap[h[pos]];}return cost-lv;
}
int isap(int st,int ed)
{s=st;t=ed;int ret=0;memset(gap,0,sizeof(gap));memset(h,0,sizeof(h));gap[st]=n;while (h[st]<n)ret+=dfs(st,INT_MAX);return ret;
}
int main()
{while(cin>>m>>n){init();for(int i=0;i<m;i++){int u,v,c;scanf("%d%d%d",&u,&v,&c);insert1(u,v,c);}printf("%d\n",isap(1,n));}return 0;
}
View Code

 

补充:sap、isap(sap+gap优化)、dinic算法:

http://blog.csdn.net/sprintfwater/article/details/7913181

sap算法:

http://www.cnblogs.com/longdouhzt/archive/2011/09/04/2166187.html

转载于:https://www.cnblogs.com/pdev/p/3873789.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/273643.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

delphi读取excel

简单的例子 1 procedure TForm1.Button1Click(Sender: TObject);2 var3 ExcelApp,MyWorkBook: OLEVariant;4 begin5 opendialog1.Filter:Microsoft Excel Workbook (*.xls)|*.XLS|; 6 edit2.Text : sheet1;7 if opendialog1.Execute then8 begin9 edit1.Text:o…

Docker-compose 常用命令及网络设置(五)

Docker Compose 常用命令 build 构建或重新构建服务。服务被构建后将会以 project_service的形式标记,例如:comoretest db。help 査看指定命令的帮助文档,该命令非常实用。 docker-compose所有命令的帮助文档都可通过该命令查看。 docker-compose he lp COMMAND 示例 docker-co…

浅谈 trie树 及其实现

定义&#xff1a;又称字典树&#xff0c;单词查找树或者前缀树&#xff0c;是一种用于快速检索的多叉树结构&#xff0c; 如英文字母的字典树是一个26叉树&#xff0c;数字的字典树是一个10叉树。 核心思想&#xff1a;是空间换时间.利用字符串的公共前缀来降低查询时间的开销以…

Docker-compose 安装与基本使用(四)

安装 Docker-Compose Compose有多种安装方式,例如通过 shell, pip以及将 Compose作为容器安装等。本次安装以Shell 为主。 通过以下命令自动下载并安装适应系统版本的 Compose: curl -L "https://github.com/docker/compose/releases/download/1.10.0/docker-compose-$(un…

如何开始DDD(完)

连续写了两篇文章&#xff0c;这一篇我想是序的完结篇了。结合用户注册的例子再将他简单丰富一下。在这里只添加一个简单需求&#xff0c;就是用户注册成功后给用户发一封邮件。补充一下之前的代码 public class DomainService {public void Register(User user){if (_userRepo…

git pull 报错:Untracked Fles Preventing Merge

场景 使用 git pull 命令更新报错解决 找到对应的文件删除后重新打开项目。

关于string,我今天科普的

今天下午朋友讨论组上讨论一个关于string的问题&#xff0c;问题是这样的&#xff0c;string a"aaa";string ba;a"bbb",为什么测试b的值不改变&#xff1f;之前我看过一个文章&#xff0c;知道肯定不相等&#xff0c;因为引用地址的一系列问题&#xff0c;…

git pull 报错:The following untracked working tree files would be overwritten by merge

场景 使用 git pull 命令更新报错 Updating d652d1c..fa05549 error: The following untracked working tree files would be overwritten by merge:.idea/encodings.xmlPlease move or remove them before you can merge. Aborting 解决 使用 git clean -d -fx 命令即可。

SpringBoot 配置多数据源

项目Git地址&#xff1a;SpringBoot 配置多数据源&#xff1a;Jacob-multi-data-source 准备工作 准备两个数据库(此模块中两个数据库一个为本地 一个为远程&#xff0c;本地为主&#xff0c;远程为从)。然后建表。 #本地库 CREATE TABLE username (id bigint(11) NOT NULL AUT…

HDU 2912

直线关于球的多次反射&#xff0c;求最后一次反射点 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace std; const double inf1e10; const double eps1e-8; struct point {doub…

EMVTag系列3《持卡人基本信息数据》

9F61 持卡人证件号 L&#xff1a;2–26 R&#xff08;需求&#xff09;&#xff1a;数据应存在&#xff0c;在读应用数据过程中&#xff0c;终端不检查&#xff1b; (PBOC2.0第五部分中规定)芯片中持卡人姓名 5F20与持卡人姓名扩展9F0B只能使用一个&#xff0c;另一个必须不…

BindingException: Parameter 'XXX' not found. Available parameters are [collection, list]

应业务需求&#xff0c;需要使用到MQ进行数据上传和下发。传递格式为JSON,服务那边下发JSON数组&#xff0c;接收端将JSON数组转换成List集合&#xff0c;调用Mybatis-plus批量添加saveBatch()。提示字段未找到... org.apache.ibatis.exceptions.PersistenceException: ### Er…

JDK 8 新特性 之 default关键字

前言 Jdk1.8之前的接口中只声明方法&#xff0c;方法具体实现应在子类中进行。Jdk1.8打破了这样的用法&#xff1a;接口中可以实现具体的方法体&#xff0c;只需要加上关键字static或者default修饰即可。 default关键字 public interface UserService {//自定义方法void getUse…

headroom.js插件使用方法

1.什么是headroom.js&#xff1f; headroom是用纯Javascript写的插件&#xff0c;用来隐藏和展示页面元素&#xff0c;从而为页面留下更多空间。比如使用headroom能使导航栏当页面下滚时消失&#xff0c;当页面上滚时候又出现。&#xff08;查看效果&#xff09; 2.工作原理 通…

JDK 8 新特性 之 方法引用

概述 方法引用&#xff1a;当要传递给Lambda体的操作&#xff0c;已经有实现的方法了&#xff0c;就可以使用方法引用方法引用&#xff1a;在Lambda的基础上进一步的简化。换句话说&#xff0c;方法引用就是Lambda表达式&#xff0c;也就是函数式接口的一个实例&#xff0c;通过…

项目记录:springmvc forward redirect 问题

RequestMapping("/redirect")public String redirect(RedirectAttributes redirectAttributes){redirectAttributes.addFlashAttribute("test", "testdata"); //专供此种情况下使用。return "redirect:read";} 注意&#xff1a;此种情…

JDK 8 新特性 之 Lambda表达式

前言 Lambda 表达式&#xff0c;也可称为闭包&#xff0c;它是推动 Java 8 发布的最重要新特性。Lambda 允许把函数作为参数传递进方法中。使用 Lambda 表达式可以使代码变的更加简洁紧凑。lambda表达式的重要特征: 可选类型声明&#xff1a;不需要声明参数类型&#xff0c;编译…

开源组件DocX导出Word

1、使用Docx替换Word模板里书签里内容的一个方法 using Novacode;public class ExportWord{/// <summary>/// 导出word/// </summary>/// <param name"lBookMarks">书签数据源</param>/// <param name"sTemplatePath">导出W…

JDK 8 新特性 之 Strams简单使用

概述 Java 8 API添加了一个新的抽象称为流Stream&#xff0c;可以让你以一种声明的方式处理数据。 Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。 Stream API可以极大提供Java程序员的生产力&#xff0c;让程序员写出…

Cannot open include file: jni.h: No such file or directory解决方法

在此运行Visual Studio 2012 项目时出现 #include <stdio.h> #include <jni.h> int main() { printf("Hello World"); } But when I try to build, I get the following error - 1>c:testtest.cpp(2) : fatal error C1083: Cannot open include file:…