poj 1273 Drainage Ditches (第一道网络流~)

来源:互联网 发布:搜索东西的软件 编辑:程序博客网 时间:2024/06/06 00:22

网络流入门题,注意可能有重边。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int maxn = 210;int c[maxn][maxn], pre[maxn], f[maxn][maxn];int n, m, add;bool bfs(){    queue<int> Q;    memset(pre, -1, sizeof(pre));    add = 1000000000;    Q.push(1);    pre[1] = 0;    while (!Q.empty())    {        int cur = Q.front();        Q.pop();        for (int i = 1; i <= m; ++i)        {            if (pre[i] == -1 && c[cur][i] > f[cur][i])            {                pre[i] = cur;                add = min(add, c[cur][i] - f[cur][i]);                if (i == m) return true;                Q.push(i);            }        }    }    return false;}int maxFlow(){    int ret = 0;    while (bfs())    {        ret += add;        int p = m;        while (pre[p] != 0)        {            f[pre[p]][p] += add;            f[p][pre[p]] -= add;            p = pre[p];        }    }    return ret;}int main(){    while (scanf("%d %d", &n, &m) != EOF)    {        memset(c, 0, sizeof(c));        memset(f, 0, sizeof(f));        int S, E, C;        for (int i = 0; i < n; ++i)        {            scanf("%d %d %d", &S, &E, &C);            c[S][E] += C;        }        printf("%d\n", maxFlow());    }    return 0;}

 

原创粉丝点击