//最大流模版
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. eclipse中将项目发布到tomcat的root目录

    在eclipse中,将项目直接部署在tomcat的root目录中,这样便可以直接ip:port访问项目: 项目右键->属性->web project settings

  2. PHP返回XML数据

    开发中经常会有用到XML格式数据的时候,那么用PHP怎么来将数组格式的数据用XML格式返回呢? 新建一个类文件,叫ArrayToXml.php <?php /** * 数组转XML类 * 使用方 ...

  3. 声明变量,一定要用 var!

    public static T TryGet<T>(Func<T> func, T ifError = default(T)) { try { return func(); } ...

  4. SAP连接电脑串口读数(电子称,磅等数据读取)

    这是几年前做的了,一直都不想分享出来,后来想想为了能够给大家点想法,献出来了... 这是一个电脑读称的方法,一般用COMM口连接的电子设备都可参考. 如果是对串口参数不确定的,可以网上找个串口测试工具 ...

  5. CSS3支持box-flex弹性布局

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing

    我不告诉你这个链接是什么 分析:模拟可以过,但是好烦啊..不会写.还有一个扩展欧几里得的方法,见下: 假设光线没有反射,而是对应的感应器镜面对称了一下的话 左下角红色的地方是原始的的方格,剩下的三个格 ...

  7. UITableView UITableViewCell

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. Python函数参数学习笔记

    1.Python中函数参数类型可分为五种: f(x):x为位置参数: f(x,n=2):n为默认参数,调用时可以省略参数n,如f(5); f(*args):*args表示把args这个list或tup ...

  9. python爬取数据保存入库

    import urllib2 import re import MySQLdb class LatestTest: #初始化 def __init__(self): self.url="ht ...

  10. [字符哈希] POJ 3094 Quicksum

    Quicksum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16488   Accepted: 11453 Descri ...