题目链接:https://www.luogu.org/problemnew/show/P3376

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int inf = 1e9;
const int maxn = ;
int n, m, s, t, deep[maxn], maxflow;
struct EDG{
int next, to, flow;
}edge[maxn];
int cnt = -, head[maxn], cur[maxn];
queue<int> q; void add(int u, int v, int w, bool flag)
{
edge[++cnt].next = head[u];
edge[cnt].to = v;
if(flag) edge[cnt].flow = w;
head[u] = cnt;
} bool bfs(int s, int t)
{
memset(deep, 0x7f, sizeof(deep));
while(!q.empty()) q.pop();
for(int i = ; i <= n; i++) cur[i] = head[i];
deep[s] = ;
q.push(s); while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now]; i != -; i = edge[i].next)
{
if(deep[edge[i].to] > inf && edge[i].flow)
{
deep[edge[i].to] = deep[now]+;
q.push(edge[i].to);
}
}
}
if(deep[t] < inf) return true;
else return false;
} int dfs(int now, int t, int limit)
{
if(!limit || now == t) return limit;
int flow = , f;
for(int i = cur[now]; i != -; i = edge[i].next)
{
cur[now] = i;
if(deep[edge[i].to] == deep[now]+ && (f = dfs(edge[i].to, t, min(limit, edge[i].flow))))
{
flow += f;
limit -= f;
edge[i].flow -= f;
edge[i^].flow += f;
if(!limit) break;
}
}
return flow;
}
void Dinic(int s, int t)
{
while(bfs(s,t))
maxflow += dfs(s,t,inf);
}
int main()
{
memset(head, -, sizeof(head));
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i = ; i <= m; i++)
{
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w,);
add(v,u,w,);
}
Dinic(s,t);
printf("%d",maxflow);
return ;
}

【luogu P3376 网络最大流】 模板的更多相关文章

  1. 【Luogu】P3376网络最大流模板(Dinic)

    最大流模板成为另一个被攻克的模板题. 今天QDC给我讲了一下Dinic,感觉很好懂.于是为了巩固就把这道题A掉了. 核心思想就是不断BFS分层,然后不断DFS找增广路.找不到之后就可以把答案累加输出了 ...

  2. P3376 网络最大流模板(Dinic + dfs多路增广优化 + 炸点优化 + 当前弧优化)

    ### P3376 题目链接 ### 这里讲一下三种优化的实现以及正确性. 1.dfs多路增广优化 一般的Dinic算法中是这样的,bfs() 用于标记多条增广路,以至于能一次 bfs() 出多次 d ...

  3. POJ2584 T-Shirt Gumbo——网络最大流模板

    题目:http://poj.org/problem?id=2584 像模板一样的简单题.继续使用 & 的当前弧优化和神奇的构造函数. #include<iostream> #inc ...

  4. P3376 网络流-最大流模板题(Dinic+当前弧优化)

    (点击此处查看原题) Dinic算法 Dinic算法相对于EK算法,主要区别在于Dinic算法对图实现了分层,使得我们可以用一次bfs,一次dfs使得多条增广路得到增广 普通的Dinic算法已经可以处 ...

  5. 洛谷P3376 (最大流模板)

    1 #include<bits/stdc++.h> 2 #define int long long 3 using namespace std; 4 const int maxn=5005 ...

  6. 图论算法-网络最大流【EK;Dinic】

    图论算法-网络最大流模板[EK;Dinic] EK模板 每次找出增广后残量网络中的最小残量增加流量 const int inf=1e9; int n,m,s,t; struct node{int v, ...

  7. P3376 【模板】网络最大流(luogu)

    P3376 [模板]网络最大流(luogu) 最大流的dinic算法模板(采取了多种优化) 优化 时间 inline+当前弧+炸点+多路增广 174ms no 当前弧 175ms no 炸点 249 ...

  8. Dinic最大流 || Luogu P3376 【模板】网络最大流

    题面:[模板]网络最大流 代码: #include<cstring> #include<cstdio> #include<iostream> #define min ...

  9. luogu P3376 【模板】网络最大流(no)ek

    题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...

随机推荐

  1. 023-将表单序列化为json对象

    使用jQuery将表单序列化为json对象,其中serializeJson方法的名字任意,serializeArray()这个jQuery提供的方法.this指的就是谁调用了这个方法. $.fn.se ...

  2. Steamworks and Unity – P2P多人游戏

    之前我们讨论过“如何把Steamworks.Net和Unity整合起来”,这是一个很好的开始,现在我们研究深一点,谈一谈Steam中的多人游戏.这不是教程,但是可以指导你在你的游戏中如何使用Steam ...

  3. unity读取json文件

    首先填表 [escel转json]注意,粘贴表之后,需要把最后的空行删掉 http://www.bejson.com/json/col2json/ [json格式化] http://www.bejso ...

  4. vsftpd配置文件解析

    对vsftpd配置文件详细解答. 1.默认配置: 1>允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES 2>匿名用户使用的登陆名为f ...

  5. Windows与Unix思想

    Unix与Windows的思想 Unix中的哲学是"一切皆文件",这里的一切皆文件是一个广泛的概念,有一些特殊的设备文件,在/dev目录下 物理设备在Unix中就对应一个特殊的设备 ...

  6. java实现截取6个汉字字母数字

    项目中使用到需要截取6个字(12个字母或数字),解决方法如下: /** * * @方法名称:getWordCount * @内容摘要: <截取输入字符串 大于6个后显示...> * @pa ...

  7. Sturct类型装箱时会遇到的问题

    Object在拆箱时会在栈空间生成一个临时变量.所以Struct在使用时尽量将内容都声明为readonly为好 [<Struct>] type Point= val mutable X:d ...

  8. web中的HTTP协议

    HTTP协议(HyperText Transfer Protocol 超文本传输协议),是浏览器接收web页面和发送web页面请求的标准协议.HTTP协议是基于TCP/IP协议的,版本号有Http1. ...

  9. Learn by pictures on Computer Fields

  10. css 字体样式设置大全

    css样式大全(整理版)   字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 { ...