POJ 1275 Drainage Ditches

最大流模板题,用EK算法。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define INF 9999999
int map[260][260],flow[260][260];
int EdmondsKarp(int sta,int tar)
{
    int cnt,t,i,f[260],pr[260];
    queue<int> q;
    cnt=0;
    memset(flow,0,sizeof(flow));
    while (1)
    {
        memset(f,0,sizeof(f));
        q.push(sta);
        f[sta]=INF;
        while (!q.empty())
        {
            t=q.front();
            q.pop();
            for (i=sta; i<=tar; i++)
            {
                if (f[i] == 0 && map[t][i] > flow[t][i])
                {
                    q.push(i);
                    f[i]=f[t] < map[t][i]-flow[t][i]?f[t]:map[t][i]-flow[t][i];
                    pr[i]=t;
                }
            }
        }
        if (f[tar] == 0)
            break;
        for (i=tar; i!=sta; i=pr[i])
        {
            flow[pr[i]][i]+=f[tar];
            flow[i][pr[i]]-=f[tar];
        }
        cnt+=f[tar];
    }
    return cnt;
}
int main()
{
    int n,m,i,t1,t2,t3,ans;
    while (cin>>n>>m)
    {
        memset(map,0,sizeof(map));
        for (i=0; i<n; i++)
        {
            cin>>t1>>t2>>t3;
            map[t1][t2]+=t3;
        }
        ans=EdmondsKarp(1,m); 
        printf("%d\n",ans);
    }
}


你可能感兴趣的:(POJ 1275 Drainage Ditches)