POJ1273/HDU1532算是最大流的模板题了。
最大流的算法有很多,我汇总了EK算法、SAP算法(邻接矩阵、邻接表)的模板,记录下来方便以后使用。但是这个原理emmmmm…我是真不清楚QAQ…
1.EK算法:(不常用)
#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
const int msize = 205;
const int INF=0x3f3f3f3f;
int n,m;
int a[msize][msize];
int pre[msize];
bool vis[msize];
bool BFS(int s,int t)
{
queue<int>q;
memset(pre,-1,sizeof(pre));
memset(vis,false,sizeof(vis));
pre[s]=s;
vis[s]=true;
q.push(s);
int p;
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=1;i<=n;++i)
{
if(a[p][i]>0&&!vis[i])
{
pre[i]=p;
vis[i]=true;
if(i==t)
return true;
q.push(i);
}
}
}
return false;
}
int EK(int s, int t)
{
int maxflow=0,d;
while(BFS(s,t))
{
d=INF;
for(int i=t;i!=s;i=pre[i])
d=min(d,a[pre[i]][i]);
for(int i=t;i!=s;i=pre[i])
{
a[pre[i]][i]-=d;
a[i][pre[i]]+=d;
}
maxflow+=d;
}
return maxflow;
}
int main()
{
while(cin>>m>>n)
{
memset(a,0,sizeof(a));
int x,y,z;
for(int i=0;i<m;++i)
{
cin>>x>>y>>z;
a[x][y]+=z;
}
cout<<EK(1,n)<<endl;
}
return 0;
}
2.SAP算法(邻接矩阵):
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAX=202;
int n,m;
int a[MAX][MAX];//存图
int pre[MAX];//记录当前点的前驱
int dis[MAX];//记录距离标号
int gap[MAX];//gap常数优化
int sap(int st,int en)
{
memset(pre,-1,sizeof(pre));
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0]=en;
int v,u=pre[st]=st;
int maxflow=0,aug=INF;
while(dis[st]<en)
{
for(v=1;v<=en;v++)
if(a[u][v]>0&&dis[u]==dis[v]+1)
break;
if(v<=en)
{
pre[v]=u;
u=v;
if(v==en)
{
aug=INF;
for(int i=v;i!=st;i=pre[i])
if(aug>a[pre[i]][i])
aug=a[pre[i]][i];
maxflow+=aug;
for(int i=v;i!=st;i=pre[i])
{
a[pre[i]][i]-=aug;
a[i][pre[i]]+=aug;
}
u=st;
}
}
else
{
int mindis=en;
for(v=1;v<=en;v++)
if(a[u][v]>0&&mindis>dis[v])
mindis=dis[v];
gap[dis[u]]--;
if(gap[dis[u]]==0)
break;//出现断层
dis[u]=mindis+1;
gap[dis[u]]++;
u=pre[u];
}
}
return maxflow;
}
int main()
{
while(scanf("%d%d",&m,&n)==2)
{
memset(a,0,sizeof(a));
int u,v,w;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
a[u][v]+=w;
}
printf("%d\n",sap(1,n));
}
return 0;
}
3.SAP算法(邻接表):
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAX=202;
int n,m;
struct Edge
{
int to, next, cap, flow;
}edge[MAX*MAX];
int tot,head[MAX];
int gap[MAX],dep[MAX],pre[MAX],cur[MAX];
void init()
{
tot=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[tot].to=v;edge[tot].cap=w;edge[tot].flow=0;
edge[tot].next=head[u]; head[u]=tot++;
edge[tot].to=u;edge[tot].cap=0;edge[tot].flow=0;
edge[tot].next=head[v];head[v]=tot++;
}
int sap(int st,int en,int n)
{
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u=st;
pre[u]=-1;
gap[0]=n;
int maxflow=0;
while(dep[st]<n)
{
if(u==en)
{
int mindis=INF;
for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
if(mindis>edge[i].cap-edge[i].flow)
mindis=edge[i].cap-edge[i].flow;
for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
{
edge[i].flow+=mindis;
edge[i^1].flow-=mindis;
}
u=st;
maxflow+=mindis;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
{
flag=true;
cur[u]=pre[v]=i;
u=v;
break;
}
}
if(flag)
{
u=v;
continue;
}
else
{
int mindis=n;
for(int i=head[u];i!=-1;i=edge[i].next)
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<mindis)
{
mindis=dep[edge[i].to];
cur[u]=i;
}
gap[dep[u]]--;
if(gap[dep[u]]==0)
break;
dep[u]=mindis+1;
gap[dep[u]]++;
if(u!=st)
u=edge[pre[u]^1].to;
}
}
return maxflow;
}
int main()
{
while(scanf("%d%d",&m,&n)==2)
{
init();//注意!!
int u,v,w;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
printf("%d\n",sap(1,n,n));
}
return 0;
}