poj-1273 (max flow)

Solution: pure board problem. . .

EK algorithm

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define maxn 205
#define maxx 1e12
#define ll long long
using namespace std;
ll flow[maxn];//The remaining flow of the flow to each vertex;
ll c[maxn][maxn];//residual network;
ll n,m;
ll pre[maxn];
queue <int> que;
int bfs(int s,int e)
{
    memset(pre,-1,sizeof(pre));
    while(!que.empty())
        that.pop();
    pre[s]=0;flow[s]=maxx;que.push(s);
    while(!que.empty())
    {
        int temp = que.front ();
        that.pop();
        if(temp==e)
            break;
        for(int i=1;i<=n;i++)
        {
            if(i!=s&&c[temp][i]>0&&pre[i]==-1)
            {
                pre[i]=temp;
                flow[i]=min(c[temp][i],flow[temp]);
                que.push (i);
            }
        }
    }
    if(pre[e]==-1)
        return -1;
    else
        return flow[e];
}
int maxflow(int s,int e)
{
    long long int tempsum=0;
    long long int anssum=0;
    while((tempsum=bfs(s,e))!=-1)
    {
        int k = e;
        while(k!=s)
        {
            int last=pre[k];
            c[last][k]-=tempsum;
            c[k][last]+=tempsum;
            k=last;
        }
        anssum + = tempsum;
    }

    return anssum;
}
intmain()
{
    int x,y,w;
    while(cin>>m>>n)
    {
        memset(c,0,sizeof(c));
        memset(flow,0,sizeof(flow));
        for(int i=1;i<=m;i++)
        {
            cin>>x>>y>>w;
            c[x][y]+=w;
        }
        long long int  ans=maxflow(1,n);
        cout<<ans<<endl;
    }
    return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325289651&siteId=291194637