洛谷P3376【模板】网络最大流 ISAP
这篇博客写得非常好呀。
于是我是DCOI这一届第一个网络流写ISAP的人了,之后不用再被YKK她们嘲笑我用Dinic了!就是这样!
感觉ISAP是会比Dinic快,只分一次层,然后不能增广了再更新dis,再加上杂七杂八的优化,但是写起来要比Dinic稍微复杂一点点。
洛谷的模板比我改过后的Dinic又快了两倍多。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
typedef long long LL;
const int maxn=,maxm=,INF=0x7f7f7f7f;
using namespace std;
int n,m,s,t,u,v,w,ecnt=,fir[maxn],d[maxn],cur[maxn],c[maxn],p[maxn];
struct edge {
int from,to,cap,flow,nxt;
edge(){}
edge(int from,int to,int cap,int flow,int nxt):from(from),to(to),cap(cap),flow(flow),nxt(nxt){}
}e[maxm];
void add(int u,int v,int w) {
e[++ecnt]=edge(u,v,w,,fir[u]);
e[++ecnt]=edge(v,u,,,fir[v]);
fir[u]=ecnt-; fir[v]=ecnt;
}
void init() {
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=;i<=m;i++) {
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
}
queue<int>que;
void bfs(int s,int t) {
for(int i=;i<=n;i++) d[i]=n;
d[t]=;
que.push(t);
while(!que.empty()) {
int x=que.front() ;que.pop();
for(int i=fir[x];i;i=e[i].nxt)
if(d[e[i].to]==n&&e[i].flow==e[i].cap) {
d[e[i].to]=d[x]+;
que.push(e[i].to);
}
}
}
int cal(int s,int t) {
int fl=INF;
for(int x=t;x!=s;x=e[p[x]].from)
fl=min(fl,e[p[x]].cap-e[p[x]].flow);
for(int x=t;x!=s;x=e[p[x]].from) {
e[p[x]].flow+=fl;
e[p[x]^].flow-=fl;
}
return fl;
}
int Maxflow(int s,int t) {
bfs(s,t);
int res=;
for(int i=;i<=n;i++) cur[i]=fir[i],c[d[i]]++;
for(int x=s;d[x]<n;) {
if(x==t) {
res+=cal(s,t);
x=s;
}
int ok=;
for(int &i=cur[x];i;i=e[i].nxt)
if(d[e[i].to]+==d[x]&&e[i].cap>e[i].flow){
p[x=e[i].to]=i;
ok=; break;
}
if(!ok) {
cur[x]=fir[x]; int M=n;
for(int i=cur[x];i;i=e[i].nxt)
if(e[i].cap>e[i].flow)
M=min(M,d[e[i].to]+);
if(!(--c[d[x]])) break;
c[d[x]=M]++;
if(x!=s) x=e[p[x]].from;
}
}
return res;
}
void work() {
printf("%d\n",Maxflow(s,t));
}
int main()
{
init();
work();
return ;
}
洛谷P3376【模板】网络最大流 ISAP的更多相关文章
- 【最大流ISAP】洛谷P3376模板题
题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...
- 洛谷.3254.圆桌问题(最大流ISAP)
题目链接 日常水题 还是忍不住吐槽这题奇怪的评价 #include <cstdio> #include <cctype> #include <algorithm> ...
- P3376 [模板] 网络最大流
https://www.luogu.org/blog/ONE-PIECE/wang-lao-liu-jiang-xie-zhi-dinic EK 292ms #include <bits/std ...
- [洛谷P3376题解]网络流(最大流)的实现算法讲解与代码
[洛谷P3376题解]网络流(最大流)的实现算法讲解与代码 更坏的阅读体验 定义 对于给定的一个网络,有向图中每个的边权表示可以通过的最大流量.假设出发点S水流无限大,求水流到终点T后的最大流量. 起 ...
- 洛谷 P1546 最短网络 Agri-Net
题目链接 https://www.luogu.org/problemnew/show/P1546 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当 ...
- 洛谷P1546 最短网络 Agri-Net(最小生成树,Kruskal)
洛谷P1546 最短网络 Agri-Net 最小生成树模板题. 直接使用 Kruskal 求解. 复杂度为 \(O(E\log E)\) . #include<stdio.h> #incl ...
- 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)
To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...
- 洛谷 P3376 【【模板】网络最大流】
题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行包含三个正整数ui. ...
- 『题解』洛谷P3376 【模板】网络最大流
Problem Portal Portal1:Luogu Description 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. Input 第一行包含四个正整数\(N,M,S,T\),分 ...
随机推荐
- k8s 存储 nfs服务
1.所有节点安装nfs yum install nfs-utils -y 2.配置nfs服务端,在master节点上 vim exports /data 10.0.0.0/24(rw,async,no ...
- 转载:jQuery 获取屏幕高度、宽度
做手机Web开发做浏览器兼容用到了,所以在网上找了些汇总下. alert($(window).height()); //浏览器当前窗口可视区域高度 alert($(document).height() ...
- 《DSP using MATLAB》Problem 8.45
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- WebApi的Swagger多版本控制实现
WebApi + Swagger2.0接口文档多版本控制设计实现 最近前后端分离的项目越来越多,API的对接对于前后端开发交流得最多的一块内容,一个好的API文档生成工具就显得非常重要,选取了Swag ...
- Nginx常用功能配置一
Nginx常用功能配置 参数include配置 说明:如果日常工作中server标签存在太多,可以采用include配置模式,Nginx的主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目 ...
- 使用PyCharm创建Django项目及基本配置
https://segmentfault.com/a/1190000011576316 pycharm是个很不错的python开发工具,大大缩短了python项目的创建时间以及调试时间在使用pytho ...
- Spark与Hadoop的对比
- ionic js 侧栏菜单 把主要内容区域从一边拖动到另一边,来让左侧或右侧的侧栏菜单进行切换
ionic 侧栏菜单 一个容器元素包含侧边菜单和主要内容.通过把主要内容区域从一边拖动到另一边,来让左侧或右侧的侧栏菜单进行切换. 效果图如下所示: 用法 要使用侧栏菜单,添加一个父元素<ion ...
- LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- Excel-根据分隔符将一个单元格的内容分发到多个单元格
A1 1:2:3:4:5:6:7::::10 现在想将A1根据";"进行分离,再讲分离出来的一个值填到一个单元格中 =TRIM(MID(SUBSTITUTE($A$12," ...