#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. windows10源码编译llvm

    准备 cmake, 我目前使用的版本是3.18 llvm 源码, 我下载的是 11.0 我已经具备Vs2015和Vs2017的开发环境. debug模式编译需要较多内存和较多硬盘存储空间. (debu ...

  2. nim_duilib(7)之TreeView

    introduction 更多控件用法,请参考 here 和 源码. 本文的代码基于这里 xml文件添加代码 基于上一篇, 继续向basic.xml中添加下面关于TreeView的代码. xml完整源 ...

  3. c++之sigslot库

    关于 本文演示环境: win10 + VS2017 1. demo #include <iostream> #include "sigslot.h" using nam ...

  4. 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)

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

  5. 【LeetCode】1001. Grid Illumination 解题报告(C++)

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

  6. 【九度OJ】题目1445:How Many Tables 解题报告

    [九度OJ]题目1445:How Many Tables 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1445 题目描述: ...

  7. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  8. 【LeetCode】468. Validate IP Address 解题报告(Python)

    [LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  9. computer(hdu2196)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  10. 1289 - LCM from 1 to n

    http://blog.csdn.net/acdreamers/article/details/18507767 这个是位图的链接,这篇写的挺好. 模板: 1 #include<math.h&g ...