最大流Emonds_karp:
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 8 const int INF=0xf777; 9 const int MAXN=1000; 10 11 int n,m,ans; 12 int vis[MAXN],pre[MAXN]; 13 int mp[MAXN][MAXN]; 14 15 bool bfs(int s,int t) 16 { 17 memset(vis,0,sizeof(vis)); 18 memset(pre,0,sizeof(pre)); 19 vis[s]=1; 20 queue<int> Q; 21 Q.push(s); 22 while(!Q.empty()) 23 { 24 int q=Q.front();Q.pop(); 25 if(q==t) return true; 26 for(int i=1;i<=n;i++) 27 if(!vis[i]&&mp[q][i]) 28 { 29 vis[i]=1; 30 pre[i]=q; 31 Q.push(i); 32 } 33 } 34 return false; 35 } 36 37 int Edmonds_Karp(int s,int t) 38 { 39 int ans=0; 40 while(bfs(s,t)) 41 { 42 int minn=INF; 43 for(int i=t;i!=s;i=pre[i]) 44 minn=min(minn,mp[pre[i]][i]); 45 for(int i=t;i!=s;i=pre[i]) 46 { 47 mp[pre[i]][i]-=minn; 48 mp[i][pre[i]]+=minn; 49 } 50 ans+=minn; 51 } 52 return ans; 53 } 54 55 int main() 56 { 57 scanf("%d%d",&n,&m); 58 for(int i=1;i<=m;i++) 59 { 60 int x,y,z; 61 scanf("%d%d%d",&x,&y,&z); 62 mp[x][y]=z; 63 } 64 printf("%d",Edmonds_Karp(1,n)); 65 return 0; 66 }
最大流dinic:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 7 const int INF=0x7f7f7f7f; 8 const int MAXN=400000; 9 10 struct Edge 11 { 12 int to,w,next; 13 }E[MAXN]; 14 int node=1,head[MAXN],dis[MAXN]; 15 int s,t; 16 int n,m,ans; 17 18 void insert(int u,int v,int w) 19 { 20 E[++node]=(Edge){v,w,head[u]}; 21 head[u]=node; 22 E[++node]=(Edge){u,0,head[v]}; 23 head[v]=node; 24 } 25 26 bool bfs() 27 { 28 memset(dis,-1,sizeof(dis)); 29 queue<int> Q; 30 Q.push(s); 31 dis[s]=0; 32 while(!Q.empty()) 33 { 34 int q=Q.front();Q.pop(); 35 for(int i=head[q];i;i=E[i].next) 36 if(E[i].w&&dis[E[i].to]==-1) 37 { 38 Q.push(E[i].to); 39 dis[E[i].to]=dis[q]+1; 40 } 41 } 42 return dis[t]!=-1; 43 } 44 45 int dfs(int x,int flow) 46 { 47 if(x==t) return flow; 48 int w,used=0; 49 for(int i=head[x];i;i=E[i].next) 50 if(E[i].w&&dis[E[i].to]==dis[x]+1) 51 { 52 w=flow-used; 53 w=dfs(E[i].to,min(w,E[i].w)); 54 E[i].w-=w; 55 E[i^1].w+=w; 56 used+=w; 57 if(used==flow)return flow; 58 } 59 if(!used) dis[x]=-1; 60 return used; 61 } 62 63 void dinic() 64 { 65 while(bfs()) ans+=dfs(s,INF); 66 } 67 68 int main() 69 { 70 scanf("%d%d%d%d",&n,&m,&s,&t); 71 for(int i=1;i<=m;i++) 72 { 73 int u,v,w; 74 scanf("%d%d%d",&u,&v,&w); 75 insert(u,v,w); 76 } 77 dinic(); 78 printf("%d",ans); 79 return 0; 80 }
spfa最小费用最大流:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 7 const int INF=0x7f7f7f7f; 8 const int MAXN=100000; 9 10 struct Edge 11 { 12 int u,v,w,c,next;//u起点,v终点,w容量,c花费,next下一条边 13 }E[MAXN]; 14 int node,head[MAXN]; 15 int pre[MAXN],cost[MAXN],vis[MAXN]; 16 int s,t; 17 int n,m,ans1,ans2; 18 19 void insert(int u,int v,int w,int c) 20 { 21 E[++node]=(Edge){u,v,w,c,head[u]}; 22 head[u]=node; 23 E[++node]=(Edge){v,u,0,-c,head[v]}; 24 head[v]=node; 25 } 26 27 bool spfa() 28 { 29 queue<int> Q; 30 memset(cost,0x7f,sizeof(cost)); 31 Q.push(s); 32 cost[s]=0;vis[s]=1; 33 while(!Q.empty()) 34 { 35 int q=Q.front();Q.pop(); 36 for(int i=head[q];i;i=E[i].next) 37 if(E[i].w&&cost[q]+E[i].c<cost[E[i].v]) 38 { 39 cost[E[i].v]=cost[q]+E[i].c; 40 pre[E[i].v]=i; 41 if(!vis[E[i].v]) 42 { 43 Q.push(E[i].v); 44 vis[E[i].v]=1; 45 } 46 } 47 vis[q]=0; 48 } 49 return cost[t]!=INF; 50 } 51 52 void mcf() 53 { 54 int minn=INF; 55 for(int i=pre[t];i;i=pre[E[i].u]) 56 minn=min(minn,E[i].w); 57 for(int i=pre[t];i;i=pre[E[i].u]) 58 { 59 ans2+=minn*E[i].c; 60 E[i].w-=minn; 61 E[i^1].w+=minn; 62 } 63 ans1+=minn; 64 } 65 66 int main() 67 { 68 scanf("%d%d%d%d",&n,&m,&s,&t); 69 for(int i=1;i<=m;i++) 70 { 71 int u,v,w,c; 72 scanf("%d%d%d%d",&u,&v,&w,&c); 73 insert(u,v,w,c); 74 } 75 while(spfa()) mcf(); 76 printf("%d %d",ans1,ans2);//ans1最大流 ans2最小费用 77 return 0; 78 }
zkw最小费用流:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 7 const int INF=0x7f7f7f7f; 8 const int T=601; 9 10 struct Edge 11 { 12 int from,to,v,c,next; 13 }E[1000000]; 14 int node=1; 15 int head[5000],dis[5000]; 16 bool vis[5000]; 17 18 int n,m; 19 int ans; 20 21 void ins(int from,int to,int v,int c) 22 { 23 E[++node]=(Edge){from,to,v,c,head[from]}; 24 head[from]=node; 25 } 26 void insert(int from,int to,int v,int c) 27 { 28 ins(from,to,v,c);ins(to,from,0,-c); 29 } 30 31 bool spfa() 32 { 33 memset(vis,0,sizeof(vis)); 34 memset(dis,0x7f,sizeof(dis)); 35 queue<int> Q; 36 Q.push(T); 37 dis[T]=0;vis[T]=1; 38 while(!Q.empty()) 39 { 40 int q=Q.front();Q.pop(); 41 for(int i=head[q];i;i=E[i].next) 42 if(E[i^1].v&&dis[q]-E[i].c<dis[E[i].to]) 43 { 44 dis[E[i].to]=dis[q]-E[i].c; 45 if(!vis[E[i].to]) 46 { 47 vis[E[i].to]=1; 48 Q.push(E[i].to); 49 } 50 } 51 vis[q]=0; 52 } 53 return dis[0]!=INF; 54 } 55 56 int dfs(int x,int f) 57 { 58 if(x==T) return f; 59 int w,used=0; 60 vis[x]=1; 61 for(int i=head[x];i;i=E[i].next) 62 if(!vis[E[i].to]&&E[i].v&&dis[E[i].to]==dis[x]-E[i].c) 63 { 64 w=f-used; 65 w=dfs(E[i].to,min(w,E[i].v)); 66 ans+=w*E[i].c; 67 E[i].v-=w;E[i^1].v+=w; 68 used+=w; 69 if(used==f) return f; 70 } 71 return used; 72 } 73 74 void zkw() 75 { 76 while(spfa()) 77 { 78 vis[T]=1; 79 while(vis[T]) 80 { 81 memset(vis,0,sizeof(vis)); 82 dfs(0,INF); 83 } 84 } 85 }