不断用BFS构造分层网络,用DFS增广。中途用取指的cur优化DFS。

Dinic封装模板:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long L;
const int maxn = + ;
const int maxm = + ;
const int INF = -1u >> ;
int fch[maxn], n, m;
L ans = ;
bool vis[maxn];
struct Tedge{
int from, to, cap, flow, next;
}adj[maxm];
struct Dinic{
int S, T, n, ms;
int d[maxn], cur[maxn], fch[maxn];
void init(int n){
this -> n = n;
memset(fch, -, sizeof(fch));
ms = ;
return ;
}
void AddEdge(int u, int v, int c){
adj[ms] = (Tedge){u, v, c, , fch[u]};
fch[u] = ms ++;
adj[ms] = (Tedge){v, u, , , fch[v]};
fch[v] = ms ++;
return ;
}
bool BFS(){
memset(vis, , sizeof(vis));
queue<int> Q;
Q.push(S); vis[S] = true; d[S] = ;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = fch[x]; i != -; i = adj[i].next){
Tedge& e = adj[i];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = true;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[T];
}
int DFS(int x, int a){
if(x == T || !a) return a;
int flow = , f;
for(int& i = cur[x]; i != -; i = adj[i].next){
Tedge& e = adj[i];
if(d[e.to] == d[x] + && (f = DFS(e.to, min(a, e.cap - e.flow))) > ){
flow += f;
a -= f;
e.flow += f;
adj[i ^ ].flow -= f;
if(!a) break;
}
}
return flow;
}
L Max_Flow(int S, int T){
this -> S = S;
this -> T = T;
L flow = ;
while(BFS()){
for(int i = ; i < n; i ++) cur[i] = fch[i];
flow += DFS(S, INF);
}
return flow;
}
}sol;
void read(int &x){
x = ; int sig = ; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') sig = -; ch = getchar(); }
while(isdigit(ch)) x = * x + ch - '', ch = getchar();
x *= sig; return ;
}
void init(){
read(n); read(m);
sol.init(n);
int u, v, w;
for(int i = ; i < m; i++){
read(u); read(v); read(w);
sol.AddEdge(u, v, w);
}
return ;
}
void work(){
ans = sol.Max_Flow(, n);
return ;
}
void print(){
printf("%lld\n", ans);
return ;
}
int main(){
init();
work();
print();
return ;
}

最大流Dinic的更多相关文章

  1. 网络流之最大流Dinic算法模版

    /* 网络流之最大流Dinic算法模版 */ #include <cstring> #include <cstdio> #include <queue> using ...

  2. poj-1459-最大流dinic+链式前向星-isap+bfs+stack

    title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...

  3. 网络流之最大流Dinic --- poj 1459

    题目链接 Description A power network consists of nodes (power stations, consumers and dispatchers) conne ...

  4. 网络最大流Dinic

    1.什么是网络最大流 形象的来说,网络最大流其实就是这样一个生活化的问题:现在有一个由许多水管组成的水流系统,每一根管道都有自己的最大通过水流限制(流量),超过这个限制水管会爆(你麻麻就会来找你喝茶q ...

  5. HDU 3572 Task Schedule(拆点+最大流dinic)

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. 学习笔记 --- 最大流Dinic算法

    为与机房各位神犇同步,学习下网络流,百度一下发现竟然那么多做法,最后在两种算法中抉择,分别是Dinic和ISAP算法,问过 CA爷后得知其实效率上无异,所以决定跟随Charge的步伐学习Dinic,所 ...

  7. Power Network(网络流最大流 & dinic算法 + 优化)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24019   Accepted: 12540 D ...

  8. ZOJ-2364 Data Transmission 分层图阻塞流 Dinic+贪心预流

    题意:给定一个分层图,即只能够在相邻层次之间流动,给定了各个顶点的层次.要求输出一个阻塞流. 分析:该题直接Dinic求最大流TLE了,网上说采用Isap也TLE,而最大流中的最高标号预流推进(HLP ...

  9. POJ2112_Optimal Milking(网洛流最大流Dinic+最短路Flody+二分)

    解题报告 农场有k个挤奶机和c头牛,每头牛到每一台挤奶机距离不一样,每台挤奶机每天最多挤m头牛的奶. 寻找一个方案,安排每头牛到某一挤奶机挤奶,使得c头牛须要走的全部路程中的最大路程的最小值. 要使每 ...

  10. 最大流dinic模板

    循环版,点的编号从0开始: ; ; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow; }edge[MAXM]; in ...

随机推荐

  1. 利用NSInvocation对方法进行抽象,实现对方法的加锁

    我们在实际开发中须要对离散的方式加锁实现线程安全,当然我们有多种实现方式,这仅仅是当中一种,使用起来比較方便 + (id)performSelectorWithTarget:(id)target se ...

  2. python----------反射和设计模式

    反射: 把字符串映动态射成对象内存地址. hasattr():判断一个对象里是否有对应的字符串的方法 getattr():根据字符串去获取obj对象里的对应方法的内存地址. class Dog(obj ...

  3. My way to Python - Day05 - 面向对象

    思维导图

  4. (转)DEDECMS 如何让栏目外部链接在新窗口中打开

    近遇到一个问题,就是dedecms的导航,是用外部链接的,但是原窗口打开不好看,新窗口打开好点.OK,放狗... 1. 查找模板中的head.htm将<li><a href='[fi ...

  5. asp.net使用dorpdownlist绑定无限级分类

          /// <summary>        /// 绑定父项分类        /// </summary> protected void DDLBindClass( ...

  6. Android虚拟机GenyMotion-- 遇到的问题

    问题: android studio 检测不到 genymotion 原因:没有设置genymotion的adb,也就是sdk的路径. 解决方法:打开genymotion的主页面,设置sdk的位置为你 ...

  7. expdp 备份数据库

    操作系统层面创建目录 [root@Oracle10g ~]# mkdir -p /home/oracle/db_back/ 2. 修改目录的所属用户.所属组 [root@Oracle10g ~]# c ...

  8. Swift - 08 - 元组

    //: Playground - noun: a place where people can play import UIKit // 元组就是将多个不同的值集合成一个数据 /* 元组是Object ...

  9. Get URL parameters & values with jQuery

    原文: http://jquery-howto.blogspot.jp/2009/09/get-url-parameters-values-with-jquery.html In this post, ...

  10. ActionResult派生类

    类名 抽象类 父类 功能 ContentResult 根据内容的类型和编码,数据内容. EmptyResult 空方法. FileResult abstract 写入文件内容,具体的写入方式在派生类中 ...