最大流Dinic

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

Dinic封装模板:

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <queue>
  7 using namespace std;
  8 typedef long long L;
  9 const int maxn = 10000 + 10;
 10 const int maxm = 80000 + 10;
 11 const int INF = -1u >> 1;
 12 int fch[maxn], n, m;
 13 L ans = 0;
 14 bool vis[maxn];
 15 struct Tedge{
 16     int from, to, cap, flow, next;
 17 }adj[maxm];
 18 struct Dinic{
 19     int S, T, n, ms;
 20     int d[maxn], cur[maxn], fch[maxn];
 21     void init(int n){
 22         this -> n = n;
 23         memset(fch, -1, sizeof(fch));
 24         ms = 0;
 25         return ;
 26     }
 27     void AddEdge(int u, int v, int c){
 28         adj[ms] = (Tedge){u, v, c, 0, fch[u]};
 29         fch[u] = ms ++;
 30         adj[ms] = (Tedge){v, u, 0, 0, fch[v]};
 31         fch[v] = ms ++;
 32         return ;
 33     }
 34     bool BFS(){
 35         memset(vis, 0, sizeof(vis));
 36         queue<int> Q;
 37         Q.push(S); vis[S] = true; d[S] = 0;
 38         while(!Q.empty()){
 39             int x = Q.front(); Q.pop();
 40             for(int i = fch[x]; i != -1; i = adj[i].next){
 41                 Tedge& e = adj[i];
 42                 if(!vis[e.to] && e.cap > e.flow){
 43                     vis[e.to] = true;
 44                     d[e.to] = d[x] + 1;
 45                     Q.push(e.to);
 46                 }
 47             }
 48         }
 49         return vis[T];
 50     }
 51     int DFS(int x, int a){
 52         if(x == T || !a) return a;
 53         int flow = 0, f;
 54         for(int& i = cur[x]; i != -1; i = adj[i].next){
 55             Tedge& e = adj[i];
 56             if(d[e.to] == d[x] + 1 && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0){
 57                 flow += f;
 58                 a -= f;
 59                 e.flow += f;
 60                 adj[i ^ 1].flow -= f;
 61                 if(!a) break;
 62             }
 63         }
 64         return flow;
 65     }
 66     L Max_Flow(int S, int T){
 67         this -> S = S;
 68         this -> T = T;
 69         L flow = 0;
 70         while(BFS()){
 71             for(int i = 0; i < n; i ++) cur[i] = fch[i];
 72             flow += DFS(S, INF);
 73         }
 74         return flow;
 75     }
 76 }sol;
 77 void read(int &x){
 78     x = 0; int sig = 1; char ch = getchar();
 79     while(!isdigit(ch)) { if(ch == ‘-‘) sig = -1; ch = getchar(); }
 80     while(isdigit(ch)) x = 10 * x + ch - ‘0‘, ch = getchar();
 81     x *= sig; return ;
 82 }
 83 void init(){
 84     read(n); read(m);
 85     sol.init(n);
 86     int u, v, w;
 87     for(int i = 0; i < m; i++){
 88         read(u); read(v); read(w);
 89         sol.AddEdge(u, v, w);
 90     }
 91     return ;
 92 }
 93 void work(){
 94     ans = sol.Max_Flow(1, n);
 95     return ;
 96 }
 97 void print(){
 98     printf("%lld\n", ans);
 99     return ;
100 }
101 int main(){
102     init();
103     work();
104     print();
105     return 0;
106 }
时间: 2024-10-27 17:13:06

最大流Dinic的相关文章

最大流 Dinic + Sap 模板

不说别的,直接上模板. Dinic+当前弧优化: struct Edge{ int x,y,c,ne; }e[M*2]; int be[N],all; int d[N],q[N]; int stack[N],top;//栈存的是边 int cur[N];//当前弧优化 void add(int x, int y, int z)//需保证相反边第一个为偶数 { e[all].x=x; e[all].y=y; e[all].c=z; e[all].ne=be[x]; be[x]=all++; e[a

【算法】网络最大流 Dinic

Dinic的大体思路是和EK差不多的(其实很多算法的大体思路都一样),只不过Dinic在每次寻找增广路时先bfs一下,给每个点都加上一个等级,而规定:只有等级相邻的两个点之间才能走,那么在dfs时就会减掉很多无用因此不必要的道路 1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 using na

POJ训练计划1459_Power Network(网络流最大流/Dinic)

解题报告 这题建模实在是好建,,,好贱,,, 给前向星给跪了,纯dinic的前向星竟然TLE,sad,,,回头看看优化,,, 矩阵跑过了,2A,sad,,, /************************************************************************* > File Name: PowerN.cpp > Author: _nplus > Mail: [email protected] > Time: 2014年07月19日 星期

网络最大流 dinic算法

一句话题意:给出一个网络图,以及其源点和汇点,求出其网络最大流 //dinic算法; //时间复杂度O(V^2E); #include<bits/stdc++.h> #define inf 999999 #define maxn 200000 using namespace std; int n,m,s,t; int ans=0; struct Edge { int to,next,w; }; struct Edge edge[maxn]; int head[maxn],val[maxn],p

poj-1459-最大流dinic+链式前向星

title: poj-1459-最大流dinic+链式前向星 date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM-网络流-最大流 概述 这道是一道网络流里最大流的板子题,,, 暑期集训网络流草草水过,,连基本的算法都不知道有哪些,,,更别提怎么实现了,,,只知道网络流的大致的概念,, 今天花了一天的时间重新学习了一波,,,本以为这东西很简单,,,没想到不仅算法的实现一大堆的东西,,就连题目都有时候看不懂,,,,感受就是网络流的题不仅算法实

POJ 3281 Dining 最大流 Dinic算法

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10768   Accepted: 4938 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulo

POJ 1149 PIGS (网络最大流 Dinic 建对图你就赢了)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17598   Accepted: 7977 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

【uva 753】A Plug for UNIX(图论--最大流 Dinic)

题意:有N个插头,M个设备和K种转换器.要求插的设备尽量多,问最少剩几个不匹配的设备. 解法:给读入的各种插头编个号,源点到设备.设备通过转换器到插头.插头到汇点各自建一条容量为1的边.跑一次最大流就可得到最多匹配的设备数. 注意啊,我这个代码在神奇?的地方死循环了——判断队列为空的语句......Σ( ° △ °|||)︴所以大家主要看“行文思路” ? :-) 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring

POJ 3281 Dining(最大流dinic&amp;&amp;SAP)

Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although

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

解题报告 农场有k个挤奶机和c头牛,每头牛到每一台挤奶机距离不一样,每台挤奶机每天最多挤m头牛的奶. 寻找一个方案,安排每头牛到某一挤奶机挤奶,使得c头牛须要走的全部路程中的最大路程的最小值. 要使每一头牛都去挤奶,那么建完模型就要推断是否满流. 因为是多源多点的网络,如果源点0,汇点n+1(n=k+c) 源点到每一头牛的容量为1,每一台机器到汇点的容量为m;用flody求出随意一头牛到随意一台机器的最短路; 对于取最大距离的最小值能够用二分来找. #include <iostream> #i