zoukankan      html  css  js  c++  java
  • 最大流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
    ", ans);
     99     return ;
    100 }
    101 int main(){
    102     init();
    103     work();
    104     print();
    105     return 0;    
    106 }
  • 相关阅读:
    java基础-对象
    java基础-类
    java基础-数组
    java基础-for循环、while循环相关
    java基础-程序执行流程之if-else语句
    ODBC, OLEDB, ADO, ADO.NET
    无题
    优秀资源
    SSRS Report Knowledge Base
    SSRS 通过Customer Code访问Dataset
  • 原文地址:https://www.cnblogs.com/chxer/p/4392586.html
Copyright © 2011-2022 走看看