http://acm.hdu.edu.cn/showproblem.php?pid=3549
Ford-Fulkerson算法.
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <string> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue> #pragma comment(linker, "/STACK:102400000,102400000") #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long #define inf 0x7f7f7f7f #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define L(x) (x) << 1 #define R(x) (x) << 1 | 1 #define MID(l, r) (l + r) >> 1 #define Min(x, y) (x) < (y) ? (x) : (y) #define Max(x, y) (x) < (y) ? (y) : (x) #define E(x) (1 << (x)) #define iabs(x) (x) < 0 ? -(x) : (x) #define OUT(x) printf("%I64d\n", x) #define lowbit(x) (x)&(-x) #define Read() freopen("a.txt", "r", stdin) #define Write() freopen("b.txt", "w", stdout); #define maxn 1010 #define maxv 1010 #define mod 1000000000 using namespace std; struct edge { int to,cap,rev; edge(){} edge(int x,int y,int z) { to=x; cap=y; rev=z; } }; vector<edge>G[maxv]; bool used[maxv]; void add_edge(int from,int to,int cap) { G[from].push_back((edge){to,cap,G[to].size()}); G[to].push_back((edge){from,0,G[from].size()-1}); } int dfs(int v,int t,int f) { if(v==t) return f; used[v]=true; for(int i=0;i<G[v].size();i++) { edge &e=G[v][i]; if(!used[e.to]&&e.cap>0) { int d=dfs(e.to,t,min(f,e.cap)); if(d>0) { e.cap-=d; G[e.to][e.rev].cap+=d; // printf("%d %d\n",e.to,e.rev); return d; } } } return 0; } int max_flow(int s,int t) { int flow=0; for(;;) { memset(used,0,sizeof(used)); int f=dfs(s,t,inf); if(f==0) return flow; flow+=f; } } int main() { //freopen("a.txt","r",stdin); int t,n,m,a,b,c,j=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<=n;i++) G[i].clear(); for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); add_edge(a,b,c); } printf("Case %d: %d\n",j++,max_flow(1,n)); } return 0; }
Dinic算法:(动态邻接表)
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 #include <string> 9 #include <set> 10 #include <functional> 11 #include <numeric> 12 #include <sstream> 13 #include <stack> 14 #include <map> 15 #include <queue> 16 #pragma comment(linker, "/STACK:102400000,102400000") 17 #define CL(arr, val) memset(arr, val, sizeof(arr)) 18 19 #define ll long long 20 #define inf 0x7f7f7f7f 21 #define lc l,m,rt<<1 22 #define rc m + 1,r,rt<<1|1 23 #define pi acos(-1.0) 24 25 #define L(x) (x) << 1 26 #define R(x) (x) << 1 | 1 27 #define MID(l, r) (l + r) >> 1 28 #define Min(x, y) (x) < (y) ? (x) : (y) 29 #define Max(x, y) (x) < (y) ? (y) : (x) 30 #define E(x) (1 << (x)) 31 #define iabs(x) (x) < 0 ? -(x) : (x) 32 #define OUT(x) printf("%I64d\n", x) 33 #define lowbit(x) (x)&(-x) 34 #define Read() freopen("a.txt", "r", stdin) 35 #define Write() freopen("b.txt", "w", stdout); 36 #define maxn 1010 37 #define maxv 1010 38 #define mod 1000000000 39 using namespace std; 40 41 struct edge 42 { 43 int to,cap,rev; 44 edge(){} 45 edge(int x,int y,int z) 46 { 47 to=x; 48 cap=y; 49 rev=z; 50 } 51 }; 52 53 vector<edge>G[maxv]; 54 int level[maxv]; 55 int iter[maxv]; 56 57 void add_edge(int from,int to,int cap) 58 { 59 G[from].push_back((edge){to,cap,G[to].size()}); 60 G[to].push_back((edge){from,0,G[from].size()-1}); 61 } 62 63 void bfs(int s) 64 { 65 memset(level,-1,sizeof(level)); 66 queue<int>que; 67 level[s]=0; 68 que.push(s); 69 while(!que.empty()) 70 { 71 int v=que.front();que.pop(); 72 for(int i=0;i<G[v].size();i++) 73 { 74 edge &e=G[v][i]; 75 if(e.cap>0&&level[e.to]<0) 76 { 77 level[e.to]=level[v]+1; 78 que.push(e.to); 79 } 80 } 81 } 82 } 83 int dfs(int v,int t,int f) 84 { 85 if(v==t) return f; 86 for(int &i=iter[v];i<G[v].size();i++) 87 { 88 edge &e=G[v][i]; 89 if(e.cap>0&&level[v]<level[e.to]) 90 { 91 int d=dfs(e.to,t,min(f,e.cap)); 92 if(d>0) 93 { 94 e.cap-=d; 95 G[e.to][e.rev].cap+=d; 96 // printf("%d %d %d\n",e.to,e.rev,G[e.to][e.rev]); 97 return d; 98 } 99 } 100 } 101 return 0; 102 } 103 104 int max_flow(int s,int t) 105 { 106 int flow=0; 107 for(;;) 108 { 109 bfs(s); 110 if(level[t]<0) return flow; 111 memset(iter,0,sizeof(iter)); 112 int f; 113 while((f=dfs(s,t,inf))>0) 114 flow+=f; 115 } 116 } 117 int main() 118 { 119 //freopen("a.txt","r",stdin); 120 int t,n,m,a,b,c,j=1; 121 scanf("%d",&t); 122 while(t--) 123 { 124 scanf("%d%d",&n,&m); 125 for(int i=0;i<=n;i++) 126 G[i].clear(); 127 for(int i=0;i<m;i++) 128 { 129 scanf("%d%d%d",&a,&b,&c); 130 add_edge(a,b,c); 131 } 132 printf("Case %d: %d\n",j++,max_flow(1,n)); 133 } 134 return 0; 135 }
时间: 2024-10-26 08:29:54