Drainage Ditches

题目链接

  • 题意:

    n个点,m条边,求1点到n点的最大流

    N (0 <= N <= 200) and M (2 <= M <= 200).

struct Edge
{
    int from, to, cap, flow;
    bool operator< (const Edge& rhs) const
    {
        return from < rhs.from || (from == rhs.from && to < rhs.to);
    }
};

const int MAXV = 210;
struct ISAP
{
    int n, m, s, t;
    vector<Edge> edges;
    vector<int> G[MAXV];   // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
    bool vis[MAXV];        // BFS使用
    int d[MAXV];           // 从起点到i的距离
    int cur[MAXV];        // 当前弧指针
    int p[MAXV];          // 可增广路上的上一条弧
    int num[MAXV];        // 距离标号计数

    void AddEdge(int from, int to, int cap)
    {
        edges.push_back((Edge) { from, to, cap, 0 });
        edges.push_back((Edge) { to, from, 0, 0 });
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BFS()
    {
        memset(vis, 0, sizeof(vis));
        queue<int> Q;
        Q.push(t);
        vis[t] = 1;
        d[t] = 0;
        while(!Q.empty())
        {
            int x = Q.front();
            Q.pop();
            REP(i, G[x].size())
            {
                Edge& e = edges[G[x][i]^1];
                if(!vis[e.from] && e.cap > e.flow)
                {
                    vis[e.from] = 1;
                    d[e.from] = d[x] + 1;
                    Q.push(e.from);
                }
            }
        }
        return vis[s];
    }

    void ClearAll(int n)
    {
        this->n = n;
        REP(i, n)
            G[i].clear();
        edges.clear();
    }

    void ClearFlow()
    {
        REP(i, edges.size())
            edges[i].flow = 0;
    }

    int Augment()
    {
        int x = t, a = INF;
        while(x != s)
        {
            Edge& e = edges[p[x]];
            a = min(a, e.cap-e.flow);
            x = edges[p[x]].from;
        }
        x = t;
        while(x != s)
        {
            edges[p[x]].flow += a;
            edges[p[x]^1].flow -= a;
            x = edges[p[x]].from;
        }
        return a;
    }

    int Maxflow(int s, int t, int need)
    {
        this->s = s;
        this->t = t;
        int flow = 0;
        BFS();
        memset(num, 0, sizeof(num));
        REP(i, n) num[d[i]]++;
        int x = s;
        memset(cur, 0, sizeof(cur));
        while(d[s] < n)
        {
            if(x == t)
            {
                flow += Augment();
                if(flow >= need) return flow;
                x = s;
            }
            int ok = 0;
            FF(i, cur[x], G[x].size())
            {
                Edge& e = edges[G[x][i]];
                if(e.cap > e.flow && d[x] == d[e.to] + 1)   // Advance
                {
                    ok = 1;
                    p[e.to] = G[x][i];
                    cur[x] = i; // 注意
                    x = e.to;
                    break;
                }
            }
            if(!ok)   // Retreat
            {
                int m = n-1; // 初值注意
                REP(i, G[x].size())
                {
                    Edge& e = edges[G[x][i]];
                    if(e.cap > e.flow)
                        m = min(m, d[e.to]);
                }
                if(--num[d[x]] == 0)
                    break;
                num[d[x] = m + 1]++;
                cur[x] = 0; // 注意
                if(x != s)
                    x = edges[p[x]].from;
            }
        }
        return flow;
    }

    vector<int> Mincut()   // call this after maxflow
    {
        BFS();
        vector<int> ans;
        REP(i, edges.size())
        {
            Edge& e = edges[i];
            if(!vis[e.from] && vis[e.to] && e.cap > 0)
                ans.push_back(i);
        }
        return ans;
    }

    void Reduce()
    {
        REP(i, edges.size())
            edges[i].cap -= edges[i].flow;
    }

    void print()
    {
        printf("Graph:\n");
        REP(i, edges.size())
            printf("%d->%d, %d, %d\n", edges[i].from, edges[i].to , edges[i].cap, edges[i].flow);
    }
} mf;

int main()
{
    int n, m, a, b, v;
    while (~RII(m, n))
    {
        mf.ClearAll(n);
        REP(i, m)
        {
            RIII(a, b, v); a--; b--;
            mf.AddEdge(a, b, v);
        }
        WI(mf.Maxflow(0, n - 1, INF));
    }
    return 0;
}

Drainage Ditches,布布扣,bubuko.com

时间: 2024-10-16 01:55:32

Drainage Ditches的相关文章

POJ 1273 Drainage Ditches (网络流Dinic模板)

Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by

luogu P2740 [USACO4.2]草地排水Drainage Ditches

P2740 [USACO4.2]草地排水Drainage Ditches 2017-09-17 题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没的烦恼(不用担心,雨水会流向附近的一条小溪).作为一名一流的技师,农夫约翰已经在每条排水沟的一端安上了控制器,这样他可以控制流入排水沟的水流量. 题目描述 农夫约翰知道每一条排水沟每分钟可以流过的水量,和

hdu 1532 Drainage Ditches(最大流)

Drainage Ditches Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drai

USACO 4.2 Drainage Ditches(网络流模板题)

Drainage DitchesHal Burch Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a se

poj-1273 Drainage Ditches(最大流基础题)

题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted: 26075 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is cover

POJ 1273 -Drainage Ditches

Time Limit:1000MS    Memory Limit:10000K Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus,

USACO Section 4.2 Drainage Ditches

最大流问题.ISAP算法.注意可能会有重边,不过我用的数据结构支持重边.距离d我直接初始化为0,也可以用BFS逆向找一次. #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<queue> #define rep(i,l,r) for(int i=l;i<r;i++) #define

Drainage Ditches(最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64044   Accepted: 24718 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by