网络流Dinic模板
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
int d[maxn], head[maxn];
int n, m, s, t;
struct edge{
int u, v, f, c, next;
}Node[*maxn]; void add(int u, int v, int c, int f, int i)
{
Node[i].u = u;
Node[i].v = v;
Node[i].f = f;
Node[i].c = c;
Node[i].next = head[u];
head[u] = i;
} int bfs()
{
queue<int> Q;
mem(d,);
Q.push(s);
d[s] = ;
while(!Q.empty()){
int u = Q.front(); Q.pop();
for(int i=head[u]; i!=-; i=Node[i].next)
{
edge e = Node[i];
if(!d[e.v] && e.c > e.f)
{
d[e.v] = d[e.u] + ;
Q.push(e.v);
}
}
} return d[t] != ;
} int dfs(int u, int cap)
{
// cout<< u << " " << cap <<endl;
if(u == t)
return cap;
for(int i=head[u]; i!= -; i=Node[i].next)
{
edge e = Node[i];
if(d[e.v] == d[e.u] + && e.c > e.f)
{
int V = dfs(e.v, min(cap, e.c - e.f));
if(V > ){
Node[i].f += V;
Node[i^].f -= V;
return V;
}
}
}
return ;
}
int Dinic()
{
int ans = ;
while(bfs())
{
while(int l = dfs(s,INF))
ans += l;
}
return ans;
} int main()
{
mem(head,-);
cin>> n >> m >> s >> t;
int cnt = ;
for(int i=; i<m; i++)
{
int u, v, w;
cin>> u >> v >> w;
add(u, v, w, , cnt++);
add(v, u, , , cnt++);
}
cout<< Dinic() <<endl; return ;
}
网络流Dinic模板的更多相关文章
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- Power Network POJ - 1459 网络流 DInic 模板
#include<cstring> #include<cstdio> #define FOR(i,f_start,f_end) for(int i=f_startl;i< ...
- 网络流dinic模板,邻接矩阵+链式前向星
//这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...
- POJ 2987 Firing 最大流 网络流 dinic 模板
https://www.cnblogs.com/137shoebills/p/9100790.html http://poj.org/problem?id=2987 之前写过这道题,码一个dinic的 ...
- hdu 1532 Dinic模板(小白书)
hdu1532 输入n,m. n条边,m个点,之后给出a到b的容量,求1到m的最大流. 注意:Dinic只能调用一次,因为原理是改变cap的值,如果调用多次一样的,那么第一次会对,其余的都会是0,因为 ...
- 最大流算法 ISAP 模板 和 Dinic模板
ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...
- 网络流——SAP模板
//网络流SAP模板,复杂度O(N^2*M) //使用前调用init(源点,汇点,图中点的个数),然后调用add_edge()加边 //调用getflow得出最大流 #define N 55 #def ...
- 洛谷P3376【模板】网络最大流 Dinic模板
之前的Dinic模板照着刘汝佳写的vector然后十分鬼畜跑得奇慢无比,虽然别人这样写也没慢多少但是自己的就是令人捉急. 改成邻接表之后快了三倍,虽然还是比较慢但是自己比较满意了.虽然一开始ecnt从 ...
- ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)
//有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...
随机推荐
- Tensorflow-hub[例子解析1]
0. 引言 Tensorflow于1.7之后推出了tensorflow hub,其是一个适合于迁移学习的部分,主要通过将tensorflow的训练好的模型进行模块划分,并可以再次加以利用.不过介于推出 ...
- angularjs为ng-click事件传递参数
在angularjs开发中,我们需要为ng-click事件传递一个参数. 在js中,可以接到参数: 演示:
- Luogu P3327 [SDOI2015]约数个数和
又是恶心的莫比乌斯反演,蒟蒻我又是一脸懵逼的被CXR dalao狂虐. 题目要求\(ans=\sum_{i=1}^n \sum_{j=1}^m d(ij)\),其中\(d(ij)\)表示数\(x\)的 ...
- 第一章:模型层model layer -- Django从入门到精通系列教程
该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. 题外话: Django的教程写到这里,就进入 ...
- 2091: [Poi2010]The Minima Game
2091: [Poi2010]The Minima Game 链接 分析: 首先排序后,一定是选的连续的一段. f[i]表示前i个位置,先手-后手的最大得分. 那么考虑第i个位置是否选,如果选,先手选 ...
- ML.NET 示例:二元分类之垃圾短信检测
写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...
- Python基础(中)
前言 print(" _ooOoo_ ") print(" o8888888o ") print(" 88 . 88 ") print(&q ...
- 当给DataGrid的Itemssoure属性赋值引起TabControl_SelectionChanged事件
在TabControl的TabItem下布局了DataGrid控件时,当给dg.ItemsSource 赋值时会触发父控件的TabControl_SelectionChanged事件; 类似问题原因可 ...
- Thrift_简介(基于C#)
//Server: TProtocolFactory ProtocolFactory = new TBinaryProtocol.Factory(true, true); TTransportFact ...
- cocoapod Podfile use frameworks swift/oc混编 could not build module xxx
前置: 知名的pod: AFNetworking 我自己的pod: AFNetworking+RX 3.1.0.18 里面有一段代码是: #import <Foundation/Founda ...