//最大流模版
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. OpenBSD内核之引导MBR

    MBR的介绍网上很多,没错,就那个最后以0x55AA结尾的512字节的引导块,OpenBSD提供了引导MBR实现:OpenBSD在x86上的引导过程为MBR --> PBR --> boo ...

  2. Struts2 Result 类型和对应的用法详解

  3. JavaEE Hibernate初级概念

    1.  Hibernate 是连接Java应用程序和关系数据库的中间件: 对JDBC API进行了封装.负责Java对象的持久化: 在三层软件架构中它位于持久层(数据访问层),封装了所有数据访问细节, ...

  4. reversing-Easy Crack

    Easy Crack 程序启动后输入任意字符会显示一个MessageBox的Incorrect Password. 打开OllyDbg,载入程序后查找到目标字符串Incorrect Password, ...

  5. java筛选法求素数

    这本身没什么,代码一堆 发来纪念下而已 本来刚学习java,编写输出100以内的素数 对于我这个有代码运行性能洁癖的人(但是本身又不懂算法)来说,不能忍 于是看了些资料 参考: http://blog ...

  6. <java基础学习>RE 基础语法

    public class MyFirstJavaProgram{ public static void main(String[] args ){ System.out.println("H ...

  7. Emmet 使用说明。

    Emmet for Sublime Text Official Emmet plugin for Sublime Text. How to install Available actions Exte ...

  8. 面试复习(C++)之堆排序

    #include <iostream> using namespace std; void Maxheap(int *a,int i,int heapSize)//最大数调整 { +;// ...

  9. VScode调试Python

    第一步,确保装上了PYTHON扩展 然后打开文件夹(这个东西必须打开文件夹才能进行调试,不能打开一个文件就调试) 打开文件夹后,那里显示没有配置,这时需要你按下F5 弹出选择环境,点击Python 它 ...

  10. jQuery LigerUI V1.2.2 (包括API和全部源码) 发布

    前言 这次版本主要对树进行了加载性能上面的优化,并解决了部分兼容性的问题,添加了几个功能点. 欢迎使用反馈. 相关链接 API:         http://api.ligerui.com/ 演示地 ...