#include<bits/stdc++.h>
using namespace std;
const int MM=4e5+5,inf=0x3f3f3f3f;
int n,m,s,t,tot=1,a;
int nxt[MM],head[MM],to[MM],w[MM];
int h[MM],e[MM],gap[MM],in[MM];
struct cmp
{
bool operator()(int a,int b) const
{
return h[a]<h[b];
}
};
priority_queue<int,vector<int>,cmp> q;
void add(int u,int v,int flow)
{
nxt[++tot]=head[u];
to[tot]=v;
w[tot]=flow;
head[u]=tot;
}
bool bfs()
{
int q[1200],l=1,r=1,now;
memset(h,0x3f,sizeof(h));
q[1]=t;h[t]=0;
while(r>=l)
{
now=q[l++];
for(int i=head[now];i;i=nxt[i])
if(w[i^1]&&h[to[i]]>h[now]+1)
h[to[i]]=h[now]+1,q[++r]=to[i];
}
return h[s]!=inf;
}
void push(int now)
{
int flow;
for(int i=head[now];i;i=nxt[i])
if(w[i]&&h[to[i]]+1==h[now])
{
flow=min(e[now],w[i]);
w[i]-=flow;w[i^1]+=flow;e[now]-=flow,e[to[i]]+=flow;
if(to[i]!=s&&to[i]!=t&&!in[to[i]])
q.push(to[i]),in[to[i]]=1;
if(!e[now])
break;
} }
void relabel(int now)
{
h[now]=inf;
for(int i=head[now];i;i=nxt[i])
if(w[i]&&h[to[i]]+1<h[now])
h[now]=h[to[i]]+1;
return;
}
void pp()
{
for(int i=1;i<=n;i++)
cout<<i<<' ';
cout<<endl;
for(int i=1;i<=n;i++)
cout<<e[i]<<' ';
cout<<endl;
}
int main()
{
cin>>n>>m>>s>>t;
int u,v,a,now;
for(int i=1;i<=m;i++)
cin>>u>>v>>a,add(u,v,a),add(v,u,0); if(!bfs())
{
cout<<0;
return 0;
}
h[s]=n;
for(int i=1;i<=n;i++)
if(h[i]<inf)
++gap[h[i]];
for(int i=head[s];i;i=nxt[i])
{
int flow=w[i];
w[i]-=flow;w[i^1]+=flow;e[s]-=flow;e[to[i]]+=flow;
if(to[i]!=s&&to[i]!=t&&!in[to[i]])
q.push(to[i]),in[to[i]]=1;
} while(!q.empty())
{
now=q.top();q.pop();in[now]=0;push(now);//cout<<now<<endl;
if(e[now])
{
if(!--gap[h[now]])
for(int i=1;i<=n;i++)
if(i!=s&&i!=t&&h[i]>h[now]&&h[i]<n+1)
h[i]=n+1;
relabel(now);++gap[h[now]];
q.push(now);in[now]=1;
}
}
cout<<e[t];
return 0;
}
~~~

网络流 HLPP 板子的更多相关文章

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

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

  2. 网络流dinic板子

    bool bfs(){ memset(deep,0,sizeof(deep)); queue<int>que; que.push(s); deep[s]=1; while(!que.emp ...

  3. 【BZOJ3876】[AHOI2014&JSOI2014] 支线剧情(无源汇有上下界网络流)

    点此看题面 大致题意: 有一张\(DAG\),经过每条边有一定时间,从\(1\)号点出发,随时可以返回\(1\)号点,求经过所有边的最短时间. 无源汇有上下界网络流 这是无源汇有上下界网络流的板子题. ...

  4. 写在SDOI2016Round1前的To Do List

    理性的整理了一下自己的不足. 计算几何啥都不会,字符串类DP毫无练习,数据结构写的不熟,数论推不出式子,网络流建模常建残: 需要达成的任务: 一.网络流: 熟练网络流的板子(之前一直仰慕zkw费用流, ...

  5. [COGS 2583]南极科考旅行

    2583. 南极科考旅行 ★★   输入文件:BitonicTour.in   输出文件:BitonicTour.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 小美要 ...

  6. SDOI 2019 R1游记

    $SDOI$ $2019$ $R1$游记 昨天才刚回来,今天就来写游记啦! Day -5: 做了一下去年省选的Day1,感觉很神仙. Day -4: 做了一下去年省选的Day2,感觉还是很神仙. Da ...

  7. 友链&&日记

    上面友链,下面日记 友人链 最喜欢galgameの加藤聚聚 初三一本&&\(ACG\)姿势比我还丰厚的yx巨巨 更喜欢galgame的shadowice czx ZigZag胖胖 文文 ...

  8. P3324 [SDOI2015]星际战争

    传送门:https://www.luogu.org/problemnew/show/P3324 首先瞅一眼数据范围,发现m, n都很小,所以就可以初步断定这是一道网络流的题. 因为题中说每一个武器只能 ...

  9. Bzoj4873 [SXOI2017]寿司餐厅

    Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 64  Solved: 45 Description Kiana最近喜欢到一家非常美味的寿司餐厅用餐.每 ...

随机推荐

  1. pdf2swf转换不成功该怎么解决啊,Process p=r.exec("D:/swf/pdf2swf.exe \""+pdfFile.getPath()+"\" -o \""+swfFile.getPath()+"\" -T 9");

    pdf2swf转换不成功该怎么解决啊,可以这样解决吧,请注意命令的用法啊:Process p=r.exec("D:/swf/pdf2swf.exe  \""+pdfFil ...

  2. MySQL查询列必须和group by字段一致吗?

    @ 目录 场景:查询各部门薪水最高的员工. 方法一: 方法二: MySQL group by是如何决定哪一条数据留下的? 分组前的数据: 那么target list和group by column不匹 ...

  3. 在react项目中使用require引入图片不生效

    如果使用create-react-app和require导入图像,require返回一个ES模块而不是字符串.这是因为在file-loader中,esModule选项是默认启用的. 用以下方式之一导入 ...

  4. IDEA配置连接(自建Maven仓库)私服并打包上传

    maven的setting.xml文件配置 在servers标签里配置 <server> <id>privete_maven</id> <!--账号密码需要与 ...

  5. Linux(centos7)设置docker服务开机自启动以及容器自启动

    docker服务开机自启动 systemctl enable docker 设置容器自启动 可以在运行的时候通过设置--restart 参数 docker run --restart always - ...

  6. IDEA中SpringBoot启动报错Error:(11, 39) java: 找不到符号

    确保不是依赖没有导入或者编码不对问题后 如果还是不行 可以试试以下方式 解决办法 将图片框中Delegate IDE build 勾选 然后重新启动即可

  7. c++指针函数和函数指针概述

    欢迎指正 代码写的不够规范: 目的是缩短文章篇幅,实际中请注意 阅读完本文, 你一定能判断和写出:指针函数和函数指针. 0.结论 A.指针函数: 函数的返回值是指针类型 B.函数指针: 函数名是一个指 ...

  8. 【LeetCode】752. Open the Lock 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】712. Minimum ASCII Delete Sum for Two Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  10. Pikachu漏洞练习-SQL-inject(三)