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. C语言Windows程序开发—CreateWindow函数介绍【第03天】

    (一)CreateWindow函数的参数介绍: HWND CreateWindow( LPCTSTR lpClassName, //Windows窗口中预定义的控件结构体,包括:BUTTON(按钮), ...

  2. C++代码理解 (强制指针转换)

    #include<iostream> using namespace std; class A { public: A() { a=; b=; c=; f=; } private: int ...

  3. ccf201703-2 STLlist

    题目:http://118.190.20.162/view.page?gpid=T56 问题描述 体育老师小明要将自己班上的学生按顺序排队.他首先让学生按学号从小到大的顺序排成一排,学号小的排在前面, ...

  4. 39-Role以及Claims授权

    asp.net core多鼓励使用claims授权 1-使用role授权 在类或方法上贴上Roles,这样就知道有user的角色才可以访问 [Authorize(Roles="user&qu ...

  5. python2.7练习小例子(二十八)

    28):题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母.     程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母. ...

  6. Lambda表达式详解【转】

    前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸夸lambda吧:简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自c#1.0后新增的最重要的功能之一 ...

  7. hive报错:Caused by: ERROR XBM0H: Directory /var/lib/hive/metastore/metastore_db cannot be created.

    在cdh集群中,删除之前的hive服务,然后将hive添加到其他节点,然后再通过hive客户端连接hive报错: Caused by: ERROR XJ041: Failed to create da ...

  8. SGU刷题之路,开始了

    0. 关于SGU的简介 SGU的网址是:acm.sgu.ru 向不了解的同学介绍一下SGU这个题库: 1. 题目难度很高,题目大多很经典. 2. 其数据范围很小,时间和空间要求也都很小,同时很精确.甚 ...

  9. 14.0 native webview H5切换

    在讲这章之前先说明一个问题,那就是 native webview 都是属于原生的...webview目前用的比较多的是谷歌内核和腾讯X5内核  H5是网页! 还是安卓市场---直接写好脚本进入个人中心 ...

  10. ADB常用指令

    adb 命令是adb程序自带的一些命令:adb shell则是调用Android系统的命令,Android系统特有的命令都放在Android设备的/system/bin目录中 MonkeyRunner ...