最大流之预流推进

依然是最基础的一题最大流:poj1273

#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
const int MAXN=212;
const int oo=0x7fffffff;
int ef[MAXN],res[MAXN][MAXN],h[MAXN];
int MaxFlow,n;
int min(int a,int b)
{
    if (a<b) return a; return b;
}
class list{
    public:
    int x;
    int h;
    bool friend operator < ( const list &a, const list &b){
         return a.h<b.h;
    }
};
void push_relablel(int s,int e)
{
    priority_queue<list> Q;
    list q;
    int u,v,p;
    MaxFlow=0;
    h[s]=n;//将源点的高度定为n
    ef[s]=oo;//源点的余流为无穷大
    ef[e]=-oo;//汇点的余流为无穷小
    q.x=s;
    q.h=h[s];
    Q.push(q);//将源点加入队列中
    while (!Q.empty())
    {
        q=Q.top();
        Q.pop();
        u=q.x;
        for (v=1;v<=n;v++)
        {
            p=min(res[u][v],ef[u]);
            if (p>0&&(u==s||h[u]==h[v]+1))
            {
                res[u][v]-=p; res[v][u]+=p;//更新残留网络
                ef[u]-=p; ef[v]+=p;//更新余流
                if (v==e) MaxFlow+=p;//到达汇点,更新最大流
                if (v!=s&&v!=e)
                {
                    q.x=v;
                    q.h=h[v];
                    Q.push(q);
                }
            }
        }
        if (u!=s&&u!=e&&ef[u]>0)//若当前点的余流不为0,继续进入队列
        {
            h[u]++;
            q.x=u;
            q.h=h[u];
            Q.push(q);
        }
    }
}
int main()
{
    int m,s,e,v;
    while (cin>>m>>n)
    {
        memset(res,0,sizeof(res));//残留网络初始化
        memset(ef,0,sizeof(ef));//余流初始化
        memset(h,0,sizeof(h));//高度初始化
        for (int i=1;i<=m;i++)
        {
            cin>>s>>e>>v;
            res[s][e]+=v;
        }
        push_relablel(1,n);
        cout<<MaxFlow<<endl;
    }
}


推荐blog:http://www.cppblog.com/mronesong/archive/2012/08/24/188111.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章