LOJ 最大流加强版

#include <bits/stdc++.h>
const int inf=0x7fffffff;
const int maxn=1210;
const int maxh=1<<20;
using namespace std;
int f[maxn][maxn];
typedef unsigned short u16;
vector<u16> out[maxn],act[maxn];
#define count cnt
struct nodelist{u16 l,r;}hlist[maxn];
u16 height[maxn],count[maxn],hlisthead[maxn],que[maxn];
short high,highact;
int excess[maxn];
int n,m,s,t;
void ins(int t,int h){
int q=hlisthead[h];
int l=q?q:t;
int r=q?hlist[q].r:t;
hlist[t].l=l;
hlist[l].r=t;
hlist[t].r=r;
hlist[r].l=t;
hlisthead[h]=t;
height[t]=h;
++count[h];
}
void del(int t,int h){
int l=hlist[t].l;
int r=hlist[t].r;
hlist[r].l=l;
hlist[l].r=r;
if(hlisthead[h]==t)
hlisthead[h]=l==t?0:l;
--count[h];
}
void ae(int u,int v,int c){f[u][v]+=c;}
void regularize(){
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j)
if(f[i][j]||f[j][i])
out[i].push_back(j),out[j].push_back(i);
}
void push(int u,int v){
int df=min(excess[u],f[u][v]);
f[u][v]-=df;f[v][u]+=df;
excess[u]-=df;excess[v]+=df;
if(excess[v]>0&&excess[v]<=df)act[height[v]].push_back(v);
}
void grel(){
fill(height,height+(n+1),n);
memset(count,0,sizeof count);
memset(hlisthead,0,sizeof hlisthead);
int ql=0,qr=0;
for(ins(t,0),que[qr++]=t;ql!=qr;){
int u=que[ql++],h=height[u]+1;
for(int v:out[u])
if(height[v]==n&&f[v][u])
ins(v,h),que[qr++]=v;
}act[0].clear();
for(int i=1;i<=n;++i)
if(excess[i]>0)act[height[i]].push_back(i);
high=highact=height[que[qr-1]];
}
void discharge(int u){
int nm=n,h=height[u];
for(int v:out[u])if(f[u][v])
if(h==height[v]+1){
push(u,v);if(!excess[u])return;
}else nm=min(nm,height[v]+1);
if(count[h]==1){
for(int i=h;i<=high;++i)
while(hlisthead[i])height[hlisthead[i]]=n,del(hlisthead[i],i);
high=h-1;
}else{
del(u,h),height[u]=nm;
if(nm==n)return;
ins(u,nm);high=max(high,highact=nm);
act[nm].push_back(u);
}
}
int hlpp(){
if(s==t)return 0;
highact=high=0;height[s]=n;
excess[s]=inf;excess[t]=-inf;
for(int i:out[s])push(s,i);grel();
for(int u;highact>=0;)if(act[highact].empty())--highact;else u=act[highact].back(),act[highact].pop_back(),discharge(u);
return excess[t]+inf;
}
int main(){
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=0,u,v,f;i<m;++i){
scanf("%d%d%d",&u,&v,&f);
ae(u,v,f);
}regularize();
printf("%d\n",hlpp());
return 0;
}

HLPP的更多相关文章

  1. 网络最大流算法—最高标号预流推进HLPP

    吐槽 这个算法.. 怎么说........ 学来也就是装装13吧.... 长得比EK丑 跑的比EK慢 写着比EK难 思想 大家先来猜一下这个算法的思想吧:joy: 看看人家的名字——最高标号预留推进 ...

  2. 最大流算法-最高标号预流推进(HLPP)

    昨天我们学习了ISAP算法,它属于增广路算法的大类.今天学习的算法是预流推进算法中很高效的一类--最高标号预流推进(HLPP). 预流推进 预流推进是一种很直观的网络流算法.如果给到一个网络流让你手算 ...

  3. HLPP算法 一种高效的网络最大流算法

    #include <algorithm> #include <cstdio> #include <cctype> #include <queue> #d ...

  4. 咕咕咕-HLPP算法

    hlpp(欢乐婆婆)算法总结 突然发现咕了好久(X) emm先大概说一下,hlpp是针对网络流算法的一种复杂度更优的算法,基于预流推进(即模拟) 复杂度上界为 n2根号m 且跑不满 (所以学会了它,可 ...

  5. [学习笔记] 网络最大流的HLPP算法

    #define \(u\)的伴点集合 与\(u\)相隔一条边的且\(u\)能达到的点的集合 \(0x00~ {}~Preface\) \(HLPP(Highest~Label~Preflow~Push ...

  6. HDU 4280 Island Transport(HLPP板子)题解

    题意: 求最大流 思路: \(1e5\)条边,偷了一个超长的\(HLPP\)板子.复杂度\(n^2 \sqrt{m}\).但通常在随机情况下并没有isap快. 板子: template<clas ...

  7. 图论--网络流--最大流 洛谷P4722(hlpp)

    题目描述 给定 nn 个点,mm 条有向边,给定每条边的容量,求从点 ss 到点 tt 的最大流. 输入格式 第一行包含四个正整数nn.mm.ss.tt,用空格分隔,分别表示点的个数.有向边的个数.源 ...

  8. 网络流--最大流--hlpp(预流推进)模板

    //500ms 秒掉洛谷推流问题 #include <algorithm> #include <iostream> #include <cstring> #incl ...

  9. 网络流 HLPP 板子

    #include<bits/stdc++.h> using namespace std; const int MM=4e5+5,inf=0x3f3f3f3f; int n,m,s,t,to ...

随机推荐

  1. Entity Framework 数据生成选项DatabaseGenerated【转】

    在EF中,我们建立数据模型的时候,可以给属性配置数据生成选项DatabaseGenerated,它后有三个枚举值:Identity.None和Computed. Identity:自增长 None:不 ...

  2. C++11中Lambda的使用

    Lambda functions: Constructs a closure, an unnamed function object capable of capturing variables in ...

  3. P2966 [USACO09DEC]牛收费路径Cow Toll Paths

    P2966 [USACO09DEC]牛收费路径Cow Toll Paths 题目描述 Like everyone else, FJ is always thinking up ways to incr ...

  4. 插件开发遇到的坑------final 型变量,编译过程被优化

    android 插件开发遇到的坑 今天遇到一个坑,pdf 插件,调用了主工程的一个静态final 字符串,但是主工程里面已经没有这个字符串了,却没有崩溃. 后来同事说,因为字符串可能已经直接被写死了. ...

  5. Python的re模块的常用方法

    一.re的match与search方法 1.re.match方法 re.match 尝试从字符串的起始位置匹配一个模式,匹配成功re.match方法返回一个匹配的对象,如果不是起始位置匹配成功的话,m ...

  6. malloc函数分配失败处理的严重性

    本次在实际测试情况下,发现程序无缘无故的异常,导致看门狗超时复位,经过排查是malloc函数分配失败的时候,依然对指针进行了操作,导致异常.以前没重视这个问题是因为,总觉的malloc基本都会成功的, ...

  7. jmeter添加自定义扩展函数之图片base64编码

    打开eclipse,新建maven工程,在pom中引入jmeter核心jar包: <!-- https://mvnrepository.com/artifact/org.apache.jmete ...

  8. c free 使用MSDN library定制

    为了不使用vc6但是还要使用visual assist的各种自动功能,决定使用c free ,但是怎么调用微软的MSDN library呢,我目前使用的版本是MSDN 1.5精简版bing自动翻译的. ...

  9. js/jquery 获取本地文件的文件路劲 获取input框中type=‘file’ 中的文件路径(转载)

     原文:http://blog.csdn.net/niyingxunzong/article/details/16989947 js/jquery 获取本地文件的文件路劲 获取input框中type= ...

  10. 微信小程序—setTimeOut定时器的坑

    原文地址: http://fanjiajia.cn/2018/06/27/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E2%80%94setTimeOu ...