最大流Dinic

來源:互聯網
上載者:User

標籤:

不斷用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 }

 

最大流Dinic

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.