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. 006---Python基本数据类型--集合

    集合 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px ...

  2. idea启动spring boot无法加载或找不到主类

    问题产生原因:moudle名称修改,导致项目启动不了 在Terminal界面中执行以下三个命令,我在执行第一个命令的时候报了一个找不到dependency的错误,把那个报错的dependency删了就 ...

  3. 2019年第十届蓝桥杯C/C++程序设计本科B组省赛 E迷宫

    试题 E: 迷宫 本题总分: 分 [问题描述] 下图给出了一个迷宫的平面图,其中标记为 的为障碍,标记为 的为可 以通行的地方. 迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它 ...

  4. mysql学习第四天(高级查询)

    -- 第七章-- 1.查询入职日期最早和最晚的日期select min(hiredate),max(hiredate)from emp -- 2.查询职位以SALES开头的所有员工平均工资,最低工资, ...

  5. 北京Uber优步司机奖励政策(3月25日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. php-configure错误解决

    configure: error: libjpeg.(a|so) not foundconfigure: error: libjpeg.(a|so) not foundln -sf libjpeg.s ...

  7. SVN被锁定如何解决?

    报错: “E:/SVN被锁定” 请进行清理操作 解决方式: 1.试着右击进行解锁 没有解决 2.试着进行清理,清理后再做更新操作 好的成功. OK能正常使用了. 2017年10月26日更新 报错: s ...

  8. 「日常训练」 Mike and Fun (CFR305D2B)

    题意(CodeForces 548B) 每次对01矩阵中的一位取反,问每次操作后,单列中最长连续1的长度. 分析 非常非常简单,但是我当时训练的时候WA了四次...无力吐槽了,人间 不值得.jpg 代 ...

  9. Thymeleaf 使用时的标签

    1 . onclick事件   <a th:onclick="'javascript:more()'" ></a> 2.引入CSS样式 <link t ...

  10. C# 结构类型与类的区别

    结构类型是值类型:类是引用类型: 内存位置不同,结构类型在应用程序的堆栈中:类对象在托管中: 是否改变源对象