//最大流模版
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. css中vw,vh单位对于UC的兼容性问题

    vw,vh单位在移动端浏览器不兼容,在网上找半天也没找到什么官方的解决方法:我就试了一下在使用到vh的单位之前添加一个用px定义的样式: 如: 当浏览器不是别100vw单位的时候 就会赋给px单位的样 ...

  2. php 使用curl模拟登录人人(校内)网

    $login_url = 'http://passport.renren.com/PLogin.do'; $post_fields['email'] = 'XXXX';$post_fields['pa ...

  3. 在Mac上安装Sublime Text3的插件

    首先安装插件管理器Package Control 打开Sublime, 按下快捷键 ctrl+', 然后粘贴下面的代码,然后按回车键: import urllib.request,os; pf = ' ...

  4. 常用doc命令

    1.查看ip:ipconfig    2.ping命令 -t 就会一直ping,使用Ctrl+c退出   3.查看路由:route print   4.如何查看端口号被占用:netstat -aon| ...

  5. 在为ListView设置adapter时出错

    为listView设置adapter,代码如下: SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.m ...

  6. Socket通信 简单实现私聊、群聊(dos命令下)

    很久以前的一个Demo,这里服务器只做转发功能,根据ID地址和端口号来标识身份,群聊和私聊只是简单实现, 服务器代码如下: import java.util.*; import java.io.*; ...

  7. 吐槽!important专用博文

    在IT公司实习了1个多月,氛围还是不错的,也算是积累了一些项目经验,同时在代码模块化.版本控制.任务优先级等方面有了更进一步的体会和理解,深刻认识到在一个团队,最重要的是沟通和负责. 嗯,说了下题外话 ...

  8. sphinx全文检索功能 | windows下测试 (一)

    前一阵子尝试使用了一下Sphinx,一个能够被各种语言(PHP/Python/Ruby/etc)方便调用的全文检索系统.网上的资料大多是在linux环境下的安装使用,当然,作为生产环境很有必要部署在* ...

  9. win环境 yii2 框架 overtrue/wechat 包 由 sys_get_temp_dir 引发的 the directory "c:\Windows" is not writable

    vendor\overtrue\wechat\src\Foundation\Application.php registerBase 方法 在初始化属性时 $this['cache'] = funct ...

  10. git入门学习

    初步学习笔记. 1.创建仓库:git init 仓库:个人理解为文件存放及版本追踪的容器,对应着一个目录,目录中包含用户的文件及git用来追踪文件版本的一系列文件. 新建并进入HelloWorld目录 ...