最大流模版 pascal
//最大流模版
const maxn=; maxm=;
type list=record num:integer; a:array[..maxn] of integer; end;
var n,m,max:longint;
r:array[..maxn,..maxn] of longint;
g:array[..maxn,..maxn] of integer;
d,cur:Array[..maxn] of integer;
h:array[..maxn] of integer;//h表示高度
L:array[..maxn*-] of list;
e:array[..maxn] of longint;//e表示盈余
Buf:Array[..] of char; procedure Init;
var i,j,a,b,w:longint;
begin
Assign(Input,'test.in');reset(Input);
readln(m,n);
fillchar(d,sizeof(d),);
fillchar(e,sizeof(e),);
fillchar(r,sizeof(r),);
for i := to m do begin
readln(a,b,w);
Inc(r[a,b],w);//r表示a,b之间还能再流多少流量
end;
Close(Input);
for i := to n do begin
for j := i+ to n do begin
if (r[i][j]>) or (r[j][i]>) then begin
Inc(d[i]); Inc(d[j]);
g[i,d[i]] := j; g[j,d[j]] := i;
end;
end;
end;
for i := to n do cur[i] := ;
for i := to *n- do L[i].num := ;
end; procedure Insert(level,x:integer);
begin
with L[level] do begin
Inc(num);
a[num] := x;
end;
end; procedure Bfs;
const limit=maxn*;
var p,q,i:integer;
x:array[..maxn] of integer;
begin
for i := to n do h[i] := limit;
x[] := n; h[n] := ; q := ; p := ;
repeat
Inc(p);
for i := to d[x[p]] do begin
if h[g[x[p],i]]=limit then begin
Inc(q); x[q] := g[x[p],i];
h[x[q]] := h[x[p]] + ;
if x[q]> then Insert(h[x[q]],x[q]);
end;
end;
until p>=q;
h[] := n;
end; procedure Push(a,b:integer);
var x:longint;
begin
if r[a,b]>e[a] then x := e[a] else x := r[a,b];
Dec(r[a,b],x); Inc(r[b,a],x);
Dec(e[a],x); Inc(e[b],x);
end; procedure Relabel(a:integer);
var i,min:integer;
begin
min := maxint;
for i := to d[a] do begin
if (r[a,g[a,i]]>) and (h[g[a,i]]<min) then min := h[g[a,i]];
end;
h[a] := min+;
end; function Check(a:integer):boolean;
begin
Check := false;
while e[a]> do begin
if cur[a]>d[a] then begin
Relabel(a); Check := true; cur[a] := ;
end else begin
if (r[a,g[a,cur[a]]]>) and (h[a]=h[g[a,cur[a]]]+) then Push(a,g[a,cur[a]])
else Inc(cur[a]);
end;
end;
end; procedure Update(level:integer);
var j,k:integer;
begin
for j := level+ to n do begin
for k := to L[j].num do begin
L[n+].a[L[n+].num+k] := L[j].a[k];
h[L[j].a[k]] := n+;
end;
Inc(L[n+].num,L[j].num);
L[j].num := ;
end;
end; procedure Flow;
var i,level:integer;
begin
level := n;
repeat
Dec(level);
with L[level] do begin
for i := num downto do begin
if Check(a[i]) then begin
if (level>) and (num=) then Update(level);
Insert(h[a[i]],a[i]);
level := h[a[i]];
a[i] := a[num]; Dec(num);
break;
end;
end;
end;
until level=;
end; procedure PreFlow;
var i,b:integer;
begin
for i := to d[] do begin
b := g[,i];
e[b] := r[,b]; Dec(e[],r[,b]);
r[b,] := e[b];
r[,b] := ;
end;
end; begin
Init;
Bfs;
PreFlow;
Flow;
writeln(e[n]);
readln;
end.
最大流模版 pascal的更多相关文章
- CFGYM 2013-2014 CT S01E03 D题 费用流模版题
题意: n行, a房间的气球,b房间的气球 i行需要的气球,与a房的距离,b房的距离 求最小距离 #include <stdio.h> #include <string.h> ...
- 最大流&最小割&费用流模版
好久都没有搞博客了.想认真写又要准备文化课期末了. ISAP 流程: 原理就是dfs找增广路. 最基础的建反向边以便反悔就不说了. 但是记录一个dep(dis)表示层数,一开始BFS(从t开始,dis ...
- poj 1273 Drainage Ditches_最大流模版
#include <iostream> #include<cstdio> #include<queue> #include<cstring> using ...
- zkw费用流模版
/************************************************************** Problem: 3876 User: wangck1998 Langu ...
- 最大流模版 dinic
朴素dinic+多路增广 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- 最大流模版 EK
EK算法基于增广路的思想,易于理解,但由于低效并不被经常使用 #include <iostream> #include <cstdio> #include <algori ...
- POJ 1273 Drainage Ditches【最大流模版】
题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量 Dinic #include<iost ...
- 洛谷 [P3381] 最小费用最大流模版
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- 费用流&网络流模版
费用流模版: #include<cstdio> #include<cstring> #include<queue> using namespace std; ;// ...
随机推荐
- Ajax回调函数返回的中文字符串乱码问题
通过ajax提交请求,返回的response所带的中文字符串一直显示为乱码,写了如下代码也无效: response.setCharacterEncoding("UTF-8"); r ...
- marquee标签实现页面内容的滚动效果
页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...
- Xcode模拟器不显示SDK版本,反而显示设备ID的解决办法
今天在应用程序中修改了Xcode app 的名称,结果导致Xcode模拟器不显示SDK版本,反而显示设备ID了,感觉特别的忧伤......如图: 进到Xcode->window->Devi ...
- PHP多条件搜索ShopNc实例
控制器部分代码: if (trim($_GET['keyword']) != '') { //echo $_GET['search_type']; exit(); switch ($_GET['sea ...
- 使用wait()与notify()实现线程间协作
调用sleep()和yield()的时候锁并没有被释放,而调用wait()将释放锁.这样另一个任务(线程)可以获得当前对象的锁,从而进入它的synchronized方法中.可以通过notify()/n ...
- cacti web页面访问 settings出错
查看apache错误日志: 错误信息Mon Dec 26 11:00:48.241653 2016] [:error] [pid 32607] [client 192.168.10.79:65009] ...
- tomcat重启脚本
#!/bin/bashPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binexport JAVA_HOME=/opt/jd ...
- 【Linux】【通信】1.ping不通
关于为什么ping不通有很多种原因,但直接的表象就网络之间没有成功进行通讯: 在构建虚拟机和win之间的交互时,主要使用了3种网络模式: 桥接bridge VMnet0 主机host VMne ...
- 手机升级到iOS10,用Xcode7.3进行真机调试方法
今天发布的正式版的iOS10,手机果断升级了,结果发现Xcode7.3不能真机调试了,原因是Xcode7.3里面没有iOS10的sdk,下面这个压缩包你可以下载下来放在你的Xcode7.3里面,当然了 ...
- Visual Studio安装及单元测试
一.安装环境 操作系统版本:Win10中文版64位 CPU:i5-4200M 2.50GHz 硬盘内存:500G 二.软件版本 Visual Studio 2013 三.安装过程 1.首先开始安装, ...