网络流算法模板

 1 //EK算法
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <queue>
 6 using namespace std;
 7 
 8 const int N = 210,INF = (int)1e9;
 9 int n,m,tot;
10 int head[N],pre[N],res[N];
11 queue<int > que;
12 struct node{
13     int to,w,nxt;
14 }e[N << 1];
15 
16 inline void init(){
17     while(!que.empty()) que.pop();
18     for(int i = 1; i <= n; ++i) res[i] = 0;
19 }
20 
21 inline void add(int u,int v,int w){
22     e[tot].to = v; e[tot].w = w; e[tot].nxt = head[u]; head[u] = tot++;
23 }
24 
25 bool bfs(int s,int t){
26     init();
27     pre[s] = -1;
28     res[s] = INF;
29     que.push(s);
30     int now,to;
31     while(!que.empty()){
32         now = que.front(); que.pop();
33         for(int o = head[now]; ~o; o = e[o].nxt){
34             to = e[o].to;
35             if(!res[to] && e[o].w){
36                 pre[to] = o;
37                 res[to] = min(e[o].w, res[now]);
38                 if(to == t) return true;
39                 que.push(to);
40             }
41         }
42     }
43     return false;
44 }
45 
46 int EK(int s,int t){
47     int ans = 0;
48     while(bfs(s,t)){
49         for(int o = pre[t]; ~o; o = pre[e[o^1].to]){
50             e[o].w -= res[t];
51             e[o^1].w += res[t];
52         }
53         ans += res[t];
54     }
55     return ans;
56 }
57 
58 int main(){
59 
60     int u,v,w;
61     while(~scanf("%d%d",&m,&n)){
62         for(int i = 1; i <= n; ++i) head[i] = -1; tot = 0;
63         for(int i = 1; i <= m; ++i){
64             scanf("%d%d%d",&u,&v,&w);
65             add(u,v,w); add(v,u,0);
66         }
67         printf("%d\n",EK(1,n));
68     }
69 
70     return 0;
71 }
 1 //Dinic算法
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <queue>
 6 using namespace std;
 7 
 8 const int N = 10010,M = 100010,inf = (int)1e9;
 9 int n,m,s,t,tot;
10 int head[N],lev[N];
11 queue<int > que;
12 struct node{
13     int to,w,nxt;
14 }e[M<<1];
15 
16 inline void add(int u,int v,int w){
17     e[tot].to = v; e[tot].w = w; e[tot].nxt = head[u]; head[u] = tot++;
18 }
19 
20 inline void init(){
21     while(!que.empty()) que.pop();
22     for(int i = 1; i <= n; ++i) lev[i] = 0;
23 }
24 
25 bool bfs(int s,int t){
26     lev[s] = 1;
27     que.push(s);
28     int now,to;
29     while(!que.empty()){
30         now = que.front(); que.pop();
31         if(now == t) return true;
32         for(int o = head[now]; ~o; o = e[o].nxt){
33             to = e[o].to;
34             if(!lev[to] && e[o].w){
35                 lev[to] = lev[now] + 1;
36                 que.push(to);
37             }
38         }
39     }
40     return false;
41 }
42 
43 int dfs(int now,int flow,int t){
44     if(now == t) return flow;
45     int to,sum = 0,tmp;
46     for(int o = head[now]; ~o; o = e[o].nxt){
47         to = e[o].to;
48         if(e[o].w && lev[now] == lev[to] - 1){
49             tmp = dfs(to,min(flow - sum, e[o].w),t);
50             e[o].w -= tmp; e[o^1].w += tmp;
51             if((sum += tmp) == flow) return sum;
52         }
53     }
54     return sum;
55 }
56 
57 int main(){
58 
59     int u,v,w;
60     scanf("%d%d%d%d",&n,&m,&s,&t);
61     for(int i = 1; i <= n; ++i) head[i] = -1;
62     for(int i = 1; i <= m; ++i){
63         scanf("%d%d%d",&u,&v,&w);
64         add(u,v,w); add(v,u,0);
65     }
66     int ans = 0;
67     while(bfs(s,t)){
68         ans += dfs(s,inf,t);
69         init();
70     }
71     printf("%d\n",ans);
72 
73     return 0;
74 }

猜你喜欢

转载自www.cnblogs.com/SSummerZzz/p/12234672.html
今日推荐