板子网络流
Posted asumi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了板子网络流相关的知识,希望对你有一定的参考价值。
最大流:
例题: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 ", 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 ", SAP(S, T)); 101 } 102 return 0; 103 }
以上是关于板子网络流的主要内容,如果未能解决你的问题,请参考以下文章