题目链接: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. 安装cloudermanager时出现org.spingframework.web.bind.***** host[] is not present at AnnotationMethodHandlerAdapter.java line 738 ****错误(图文详解)(博主推荐)

    不多说,直接上干货! 首先,这个问题,写给需要帮助的朋友们,本人在此,搜索资料近半天,才得以解决.看过国内和国外,资料甚少.特此,写此博客,为了弥补此错误解决的资料少的缘故! 问题详解  解决办法   ...

  2. vi编辑器备忘录

    1. 基本操作 G 移动到文件最后一行 nG 移动到n行 gg 移动到第一行 N[Enter] 向下移动n行 /word 向下寻找 ?word 向上寻找 n 查找下一个 N 查找上一个 0或者Home ...

  3. Linux虚拟内存系统详解

    本文章以Linux为例,讲解一下虚拟内存系统的工作原理,windows系统的原理也是大同小异,有兴趣的读者可以自行查阅相关资料. linux内核以及它管理用户内存的机制,下面我们以应用程序gonzo的 ...

  4. 九度oj题目1181:遍历链表

    题目1181:遍历链表 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2600 解决:1125 题目描述: 建立一个升序链表并遍历输出. 输入: 输入的每个案例中第一行包括1个整数:n(1 ...

  5. poj 1080 ——Human Gene Functions——————【最长公共子序列变型题】

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17805   Accepted:  ...

  6. Ajax的XMLHttpRequest对象

    编写一个例子:从服务器取回一个Hello Ajax字符串. HTML: <input type="button" value="ajax提交" oncli ...

  7. Unity手册-Unity概述

    Unity概述 Unity是一个强大引擎,他自带的多种工具可以满足你多种需求. 这种编辑器是直观的可定制的,让你的工作更大的自由. 原文 Unity is a powerful engine with ...

  8. 2017年10月29日 数据库查询总结&45道题

    日期函数: 当前时间:GetDate() 两个时间差:DateDiff() 一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Tea ...

  9. c# 字体库跨域解决

    网上大部分的资料说的都是在apache和ng服务器的情况下解决方案,但基本的思路都是添加响应头 场景: 页面引用css文件: <link href="http://www.tuohua ...

  10. 字符串(1)——Detect Capital

    Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...