![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
sap+gap优化,采用邻接矩正
时间复杂度:O(mn^2)
*/
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include<iostream>
#include<queue>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
const int msize=205; //最大顶点数
const int inf=0x7fffffff;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int dis[msize]; //标号
int r[msize][msize]; //残留网络,初始为原图
int num[msize]; //num[i]表示标号为i的顶点数有多少
int pre[msize];
int n,m,src,des; //n个顶点,m条边,源点src,汇点des
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void rev_bfs() //反向bfs计算标号,汇点des标号为0
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int i,k;
for(i=1;i<=n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dis[i]=n;
num[i]=0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
queue<int> q;
q.push(des);
dis[des]=0;
num[0]=1;
while(!q.empty())
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
k=q.front();
q.pop();
for(i=1;i<=n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(dis[i]==n&&r[i][k]>0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dis[i]=dis[k]+1;
q.push(i);
num[dis[i]]++;
}
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int findArc(int i)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
for(int j=1;j<=n;j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(r[i][j]>0&&dis[i]==dis[j]+1)
return j;
}
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int reLable(int i)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int mindis=n;
for(int j=1;j<=n;j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(r[i][j]>0)
mindis=mindis<dis[j]? mindis:dis[j];
}
return mindis;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int maxFlow()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
rev_bfs();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int totalflow=0, i=src, j, k;
int d; //增量
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
pre[src]=-1;
while(dis[src]<n)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
j=findArc(i);
if(j>=0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pre[j]=i;
i=j;
if(i==des)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
d=inf;
for(k=des;k!=src;k=pre[k])
d=d<r[pre[k]][k]? d:r[pre[k]][k];
for(k=des;k!=src;k=pre[k])
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
r[pre[k]][k]-=d;
r[k][pre[k]]+=d;
}
totalflow+=d;
i=src;
}
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
--num[dis[i]];
if(0==num[dis[i]]) return totalflow;
int x=reLable(i);
dis[i]=x+1;
num[dis[i]]++;
if(i!=src) i=pre[i];
}
}
return totalflow;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
while(scanf("%d%d",&m,&n)!=EOF)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i;
int u,v,w;
memset(r,0,sizeof(r));
for(i=0;i<m;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scanf("%d%d%d",&u,&v,&w);
r[u][v]+=w;
}
src=1;
des=n;
printf("%d\n",maxFlow());
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
posted on 2011-05-10 13:12
wuxu 阅读(456)
评论(0) 编辑 收藏 引用 所属分类:
图论