HLPP
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的更多相关文章
- 网络最大流算法—最高标号预流推进HLPP
吐槽 这个算法.. 怎么说........ 学来也就是装装13吧.... 长得比EK丑 跑的比EK慢 写着比EK难 思想 大家先来猜一下这个算法的思想吧:joy: 看看人家的名字——最高标号预留推进 ...
- 最大流算法-最高标号预流推进(HLPP)
昨天我们学习了ISAP算法,它属于增广路算法的大类.今天学习的算法是预流推进算法中很高效的一类--最高标号预流推进(HLPP). 预流推进 预流推进是一种很直观的网络流算法.如果给到一个网络流让你手算 ...
- HLPP算法 一种高效的网络最大流算法
#include <algorithm> #include <cstdio> #include <cctype> #include <queue> #d ...
- 咕咕咕-HLPP算法
hlpp(欢乐婆婆)算法总结 突然发现咕了好久(X) emm先大概说一下,hlpp是针对网络流算法的一种复杂度更优的算法,基于预流推进(即模拟) 复杂度上界为 n2根号m 且跑不满 (所以学会了它,可 ...
- [学习笔记] 网络最大流的HLPP算法
#define \(u\)的伴点集合 与\(u\)相隔一条边的且\(u\)能达到的点的集合 \(0x00~ {}~Preface\) \(HLPP(Highest~Label~Preflow~Push ...
- HDU 4280 Island Transport(HLPP板子)题解
题意: 求最大流 思路: \(1e5\)条边,偷了一个超长的\(HLPP\)板子.复杂度\(n^2 \sqrt{m}\).但通常在随机情况下并没有isap快. 板子: template<clas ...
- 图论--网络流--最大流 洛谷P4722(hlpp)
题目描述 给定 nn 个点,mm 条有向边,给定每条边的容量,求从点 ss 到点 tt 的最大流. 输入格式 第一行包含四个正整数nn.mm.ss.tt,用空格分隔,分别表示点的个数.有向边的个数.源 ...
- 网络流--最大流--hlpp(预流推进)模板
//500ms 秒掉洛谷推流问题 #include <algorithm> #include <iostream> #include <cstring> #incl ...
- 网络流 HLPP 板子
#include<bits/stdc++.h> using namespace std; const int MM=4e5+5,inf=0x3f3f3f3f; int n,m,s,t,to ...
随机推荐
- linux文件操作篇 (二) 打开和关闭文件
2.1 打开文件和关闭文件 #include <sys/types.h>#include <sys/stat.h>#include <fcntl.h> 头文件 i ...
- Qt——菜单栏、工具栏、状态栏
1.菜单栏 菜单栏的意义是将可点击触发最终事件的集中在一起,所以菜单栏中是QAction 添加菜单栏是QMainWindow的行为 QMenubar *menubar = this->addMe ...
- C++ 求阶乘
#include<iostream> using namespace std; ; //输入阶乘数 int main() { long long factorial[size]; fact ...
- 【Leetcode】709. To Lower Case
To Lower Case Description Implement function ToLowerCase() that has a string parameter str, and retu ...
- RelativeSource设定绑定方向
<Window x:Class="Yingbao.Chapter2.RelativeEx.AppWin" xmlns="http://schemas.microso ...
- Git中从远程的分支获取最新的版本到本地——两种命令
Git中从远程的分支获取最新的版本到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge Git fetch origin master git log ...
- 思杰VDI提示“The VDI is not available”
前言:困扰已久的问题终于解决. 问题:客户反馈无法连接VDI. 解决过程:1.登录后台查看VDI状态为关机状态尝试重新启动提示如下图: 2.判断此VDI的启动盘出现问题(注:本人环境无数据盘) 3.查 ...
- 解析车辆VIN码识别(车架号识别)系统
很多人在购买车辆的时候,只关注性能.外观.内饰等,其实真正的内行是首先看车辆的VIN码,也叫车架号码. VIN码(车架号码)是一辆车的唯一身份证明,一般在车辆的挡风玻璃处,有的在车辆防火墙上,或B柱铭 ...
- python接口测试(一)——http请求及token获取
使用python对当前的接口进行简单的测试 1.接口测试是针对软件对外提供服务得接口得输入输出进行得测试,验证接口功能与接口描述文档得一致性 返回结果可以为字符串,json,xml等 2.接口的请求方 ...
- python中logging的常用方法
logging常用 # -*- coding:utf-8 -*- __author__ = "lgj" import os import sys import time impor ...