//最大流模版
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的更多相关文章

  1. CFGYM 2013-2014 CT S01E03 D题 费用流模版题

    题意: n行, a房间的气球,b房间的气球 i行需要的气球,与a房的距离,b房的距离 求最小距离 #include <stdio.h> #include <string.h> ...

  2. 最大流&最小割&费用流模版

    好久都没有搞博客了.想认真写又要准备文化课期末了. ISAP 流程: 原理就是dfs找增广路. 最基础的建反向边以便反悔就不说了. 但是记录一个dep(dis)表示层数,一开始BFS(从t开始,dis ...

  3. poj 1273 Drainage Ditches_最大流模版

    #include <iostream> #include<cstdio> #include<queue> #include<cstring> using ...

  4. zkw费用流模版

    /************************************************************** Problem: 3876 User: wangck1998 Langu ...

  5. 最大流模版 dinic

    朴素dinic+多路增广 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  6. 最大流模版 EK

    EK算法基于增广路的思想,易于理解,但由于低效并不被经常使用 #include <iostream> #include <cstdio> #include <algori ...

  7. POJ 1273 Drainage Ditches【最大流模版】

    题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量 Dinic #include<iost ...

  8. 洛谷 [P3381] 最小费用最大流模版

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  9. 费用流&网络流模版

    费用流模版: #include<cstdio> #include<cstring> #include<queue> using namespace std; ;// ...

随机推荐

  1. python简介和入门

    一.什么是python? python是一种面向对象.解释型的计算机语言,它的特点是语法简洁.优雅.简单易学. 二.解释型语言和编译型语言 编译型语言--就是先把写好的程序翻译成计算机语言然后执行,就 ...

  2. 从下往上看--新皮层资料的读后感 第四部分 来自神经元的设计-perceptron 感知机

    搬地方了,其他的部分看知乎:https://zhuanlan.zhihu.com/p/22114481 直到50年代,perceptron被Frank Rosenblatt搞了出来.perceptro ...

  3. Win8开虚拟wifi ‘无法启动承载网络 组或资源的状态不是执行请求操作的正确状态“

    第一步,首先我们点开开始按钮菜单,要右键以“管理员身份”打开CMD“命令提示符”并键入或者复制(粘贴)命令:netsh wlan show drivers 查看本机无线网卡是否支持此项Wifi热点共享 ...

  4. JAVA求集合中的组合

    好几个月没弄代码了,今天弄个求组合的DEMO 思路是将集合的每个值对照一个索引,索引大小是集合的大小+2.索引默认为[000...000],当组合后选取的组合值demo为[0100..00].然后根据 ...

  5. Jaxb annotation使用

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...

  6. 基于C#在WPF中使用斑马打印机进行打印【转】

    原文链接:http://ju.outofmemory.cn/entry/132476 最近在项目中接手了一个比较有挑战性的模块——用斑马打印机将需要打印的内容打印出来.苦苦折腾了两天,总算有所收获,就 ...

  7. swift-分支语句

    // switch的基本用法 // 1>switch后面的()可以省略 // 2>case中语句结束后不需要跟break // 3>在case中定义局部变量不需要跟{} // 4&g ...

  8. C# MD5加密

    public static string Encrypt(string txt) { System.Security.Cryptography.MD5CryptoServiceProvider md5 ...

  9. Unity3D之C# yield waitforseconds

    Wait for seconds requires a couple things in order to work properly and chances are if it's not work ...

  10. linux下如何使用sftp命令

    sftp 是一个交互式文件传输程式.它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性.下边就简单介绍一下如何远程连接主机,进行文件的上传和下载,以及一些相关操作. 举例,如远程主机的 IP ...