码迷,mamicode.com
首页 > 其他好文 > 详细

【板子】网络流

时间:2018-10-07 20:25:08      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:网络流   mapping   algo   blank   names   eof   else   sans   ++   

最大流:

例题:http://poj.org/problem?id=1273

dicnic:

 1 //dicnic
 2 
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <string.h>
 6 #include <cstdio>
 7 #include <queue>
 8 using namespace std;
 9 const int inf = 0x3f3f3f3f;
10 const int maxn = 205;
11 const int maxm = maxn*maxn;
12 struct node{int w; int v, next;} edge[maxm];
13 int pre[maxn], rec[maxn], head[maxn], block[maxn];
14 int dis[maxn];
15 int n, m, no;
16 int S, T;
17 queue<int> q;
18 inline void init(){
19     no = 0;
20     memset(head, -1, sizeof head);
21 }
22 inline void add(int u, int v, int w){
23     edge[no].v = v; edge[no].w = w;
24     edge[no].next = head[u]; head[u] = no++;
25     edge[no].v = u; edge[no].w = 0;
26     edge[no].next = head[v]; head[v] = no++;
27 }
28 void reset(int S, int T){
29     memset(dis, 0x3f, sizeof dis);
30     memset(block, 0, sizeof block);
31     q.push(S); dis[S] = 0;
32     while(!q.empty()){
33         int top = q.front(); q.pop();
34         for(int k = head[top]; k != -1; k = edge[k].next)
35         if(dis[edge[k].v] == inf && edge[k].w)
36             dis[edge[k].v] = dis[top]+1, q.push(edge[k].v);
37     }
38 }
39 int dinic(int S, int T){
40     int ans = 0, flow = inf;
41     int top = S;
42     reset(S, T); pre[S] = S;
43     while(dis[T] != inf){
44         int k, tmp;
45         for(k = head[top]; k != -1; k = edge[k].next){
46             if(edge[k].w && dis[edge[k].v]==dis[top]+1 && 
47             !block[edge[k].v]) break;
48         }
49         if(k != -1){
50             tmp = edge[k].v;
51             flow = min(flow, edge[k].w);
52             pre[tmp] = top, rec[tmp] = k;
53             top = tmp;
54             if(top == T){
55                 ans += flow; tmp = -1;
56                 for(; top != S; top = pre[top]){
57                     edge[rec[top]].w -= flow;
58                     edge[rec[top]^1].w += flow;
59                     if(!edge[rec[top]].w) tmp = top;
60                 }
61                 flow = inf;
62                 if(tmp != -1){
63                     top = pre[tmp];
64                     for(; top != S; top = pre[top])
65                     flow = min(flow, edge[rec[top]].w);
66                     top = pre[tmp];
67                 }
68             }
69         }
70         else{
71             block[top] = 1;
72             top = pre[top];
73             if(block[S]) reset(S, T);
74         }
75     }
76     return ans;
77 }
78 void mapping(){
79     int u, v, w;
80     for(int i = 1; i <= m; ++i){
81         scanf("%d %d %d", &u, &v, &w);
82         add(u, v, w);
83     }
84 }
85 int main(){
86     while(~scanf("%d %d", &m, &n)){
87         S = 1, T = n;
88         init();
89         mapping();
90         printf("%d\n", dinic(S, T));
91     }
92     return 0;
93 }

 

sap:

 

  1 #include <algorithm>  
  2 #include <iostream>  
  3 #include <string.h>  
  4 #include <cstdio>  
  5 #include <queue>  
  6 using namespace std;  
  7 const int inf = 0x3f3f3f3f;  
  8 const int maxn = 205;  
  9 const int maxm = maxn*maxn;
 10 struct node{int w; int v, next;} edge[maxm];  
 11 int pre[maxn], rec[maxn], head[maxn], gap[maxn], now[maxn];  
 12 int dis[maxn];
 13 int n, m, no, up;  
 14 int S, T; 
 15 queue<int> q;
 16 inline void add(int u, int v, int w)  {  
 17     edge[no].v = v; edge[no].w = w;  
 18     edge[no].next = head[u]; head[u] = no++;  
 19     edge[no].v = u; edge[no].w = 0;  
 20     edge[no].next = head[v]; head[v] = no++;  
 21 }  
 22 inline void pre_init(){  
 23     no = 0;
 24     memset(head, -1, sizeof head);  
 25 }  
 26 void init(int S, int T){  
 27     memset(gap, 0, sizeof gap);  
 28     memset(dis, 0x3f, sizeof dis);  
 29     for(int i = 0; i <= up; ++i)   
 30     now[i] = head[i];
 31     while(!q.empty()) q.pop();  
 32     dis[T] = 0; q.push(T);  
 33     while(!q.empty()){  
 34         int tp = q.front(); q.pop();  
 35         ++gap[dis[tp]];  
 36         int k = head[tp];  
 37         while(k != -1){  
 38             if(dis[edge[k].v] == inf && edge[k^1].w){  
 39                 dis[edge[k].v] = dis[tp]+1;  
 40                 q.push(edge[k].v);  
 41             }  
 42             k = edge[k].next;  
 43         }  
 44     }  
 45 }  
 46 
 47 int SAP(int S, int T)  {
 48     int ans = 0, flow = inf;
 49     int top = S;  
 50     pre[S] = S; init(S, T);  
 51     while(dis[S] < up){
 52         if(top == T){  
 53             ans += flow;  
 54             while(top != S){  
 55                 edge[rec[top]].w -= flow;  
 56                 edge[rec[top]^1].w += flow;  
 57                 top = pre[top];  
 58             }  
 59             flow = inf;  
 60         }  
 61         int k = now[top];  
 62         while(k != -1){  
 63             int v = edge[k].v;  
 64             if(edge[k].w && dis[top] == dis[v]+1){  
 65                 flow = min(flow, edge[k].w);  
 66                 pre[v] = top; rec[v] = k;  
 67                 now[top] = k; top = v;  
 68                 break;  
 69             }  
 70             k = edge[k].next;  
 71         }  
 72         if(k == -1){  
 73             int mind = up;  
 74             if(--gap[dis[top]] == 0) break;
 75             int k = now[top] = head[top];
 76             while(k != -1){  
 77                 if(edge[k].w && mind>dis[edge[k].v]) mind = dis[edge[k].v];  
 78                 k = edge[k].next;  
 79             }  
 80             ++gap[dis[top] = mind+1];  
 81             top = pre[top];  
 82         }  
 83     }  
 84     return ans;  
 85 }  
 86 
 87 void mapping(){
 88     int u, v, w;
 89     for(int i = 1; i <= m; ++i){  
 90         scanf("%d %d %d", &u, &v, &w); 
 91         add(u, v, w);  
 92     }
 93 }
 94 int main()  {
 95     while(~scanf("%d %d", &m, &n)){
 96         S = 1;T = n;
 97         up = n;
 98         pre_init();  
 99         mapping();
100         printf("%d\n", SAP(S, T)); 
101     }  
102     return 0;  
103 }

 

【板子】网络流

标签:网络流   mapping   algo   blank   names   eof   else   sans   ++   

原文地址:https://www.cnblogs.com/Asumi/p/9751117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!