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. Java——equals方法---18.10.18

    一.equals方法定义 public boolean equals(Object obj)方法 //提供对象是否“相等”的逻辑 二.”equals“和“==”的区别 1.“==”比较的是两个变量本身 ...

  2. python2.7练习小例子(十三)

        13):题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5.     程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成.(1)如果这个质数恰等于 ...

  3. Yearning和inception搭建MySQL审核平台

    前言 采用开源Yearning和inception开源软件,搭建用于MYSQL审核及线上MYSQL语句更新的审核平台. 功能说明 Yearning: 基于Vue.js与Django的整套mysql-s ...

  4. elasticsearch-mathc和term的区分

    elasticsearch和mysql在思想上是有不同的,elasticsearch有分词一说,比如北京奥运分词成北京,奥运,北京奥运.分词要要考虑两点,一个是查询字符串要不要分词,还有就是原存储字段 ...

  5. dispaly:-webkit-box 布局中的坑

    dispaly:-webkit-box 具体用法 这里大家可以网上查, 这里说下里面的坑 里面的子对象设置-webkit-box-flex: 1 -webkit-box-flex: 2 时:一般两个子 ...

  6. localStorage简析

    声明:引用自http://www.cnblogs.com/st-leslie/p/5617130.html 一.什么是localStorage.sessionStorage 在HTML5中,新加入了一 ...

  7. java.sql.Date java.sql.Time java.sql.Timestamp 之比较

    java.sql.Date,java.sql.Time和java.sql.Timestamp 三个都是java.util.Date的子类(包装类). java.sql.Date是java.util.D ...

  8. VSCode 前端必备插件

    VSCode 前端必备插件 Debugger for Chrome 让 vscode 映射 chrome 的 debug功能,静态页面都可以用 vscode 来打断点调试 { "versio ...

  9. (vue01)vue环境搭建

    腾讯,百度,网易....这么大媒体平台咋老推送这么lower的信息? 你们不缺钱啊....我这么善良的孩子都别你们带坏了 强烈鄙视马化腾 强烈鄙视李彦宏 参考地址: https://segmentfa ...

  10. abs项目 - 战线拉的太长

    abs项目 - 战线拉的太长 “从项目中来,到项目中去”. 坑是踩不完的,尽量做到不要踩重复的坑就好. 最近的这个项目,从2016的8月份左右开始立项,一直做到2017的2月份,还是有很多的问题在继续 ...