poj 1273 Drainage Ditches

也是一道模板题目...敲一遍熟悉熟悉


 

#include <iostream>

#include <algorithm>

#include <cmath>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <string>

#include <vector>

#include <set>

#include <queue>

#include <stack>

#include <climits>//形如INT_MAX一类的

#define MAX 205

#define INF 0x7FFFFFFF

# define eps 1e-5

//#pragma comment(linker, "/STACK:36777216") ///传说中的外挂

using namespace std;



int cap[MAX][MAX],pre[MAX],flow[MAX];

int m,n;



int bfs(int s,int e)

{

    queue<int >q;

    memset(pre,-1,sizeof(pre));

    pre[s] = 0;

    flow[s] = INF;

    q.push(s);

    while(! q.empty())

    {

        int t = q.front();

        q.pop();

        if(t == e)

            break;

        for(int i=1; i<=m; i++)

        {

            if(pre[i] == -1 && cap[t][i] > 0 )

            {

                q.push(i);

                pre[i] = t;

                flow[i] = min(flow[t],cap[t][i]);

            }

        }

    }

    if(pre[e] == -1)

        return -1;

    return flow[e];

}



int maxflow(int s,int e)

{

    int imp=0;

    int sum=0;

    while((imp = bfs(s,e)) != -1)

    {

        int ind = e;

        while(ind != s)

        {

            cap[pre[ind]][ind] -= imp;

            cap[ind][pre[ind]] += imp;

            ind = pre[ind];

        }

        sum += imp;

    }

    return sum;

}



void init()

{

    memset(cap,0,sizeof(cap));

    memset(flow,0,sizeof(flow));

}



int main()

{

    int a,b,c;

    while(scanf("%d%d",&n,&m) != EOF)

    {

        init();

        for(int i=0; i<n; i++)

        {

            scanf("%d%d%d",&a,&b,&c);

            if(a == b)

                continue;

            cap[a][b] += c;

        }

        printf("%d\n",maxflow(1,m));

    }

    return 0;

}


 

 

你可能感兴趣的:(poj)