HDU 1532 Drainage Ditches 【最大流】

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21098    Accepted Submission(s): 10104


Problem 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 ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
 

Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
 

Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
 

Sample Input
 
  
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
 

Sample Output
 
  
50
#include<bits/stdc++.h>
using namespace std;
#define mms(x, y) memset(x, y, sizeof x)
#define cl clear()
#define em empty()
#define pb push_back
#define sz size()
const int MAX = 205;
const int INF = 0x3f3f3f3f;
struct edge
{
    int from, to, cap, flow;
};
int n, m;
struct Dinic
{
    int n, m, S, T;
    vector <edge> edges;
    vector <int> G[MAX];
    bool vis[MAX];
    int lev[MAX];
    int cur[MAX];
    void init(int n)
    {
        this->n = n;
        for(int i = 0; i <= n; i++)
            G[i].cl;
        edges.cl;
    }
    void add(int from, int to, int cap)
    {
        edges.pb({from, to, cap, 0});
        edges.pb({to, from, 0, 0});
        m = edges.sz;
        G[from].pb(m - 2);
        G[to].pb(m - 1);
    }
    bool bfs()
    {
        mms(vis, 0);
        queue <int> q;
        q.push(S);
        lev[S] = 0;
        vis[S] = 1;
        while(!q.em)
        {
            int x = q.front();
            q.pop();
            for(int i = 0; i < G[x].sz; i++)
            {
                edge &e = edges[G[x][i]];
                if(!vis[e.to] && e.cap > e.flow)
                {
                    vis[e.to] = 1;
                    lev[e.to] = lev[x] + 1;
                    q.push(e.to);
                }
            }
        }
        return vis[T];
    }
    int dfs(int x, int a)
    {
        if(x == T || a == 0)

            return a;
        int flow = 0, f;
        for(int &i = cur[x]; i < G[x].sz; i++)
        {
            edge &e = edges[G[x][i]];
            if(lev[x] + 1 == lev[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0)
            {
                e.flow += f;
                edges[G[x][i] ^ 1].flow -= f;
                flow += f;
                a -= f;
                if(a == 0)
                    break;
            }
        }
        return flow;
    }
    int maxflow(int s, int t)
    {
        this->S = s;
        this->T = t;
        int flow = 0;
        while(bfs())
        {
            mms(cur, 0);
            flow += dfs(S, INF);
        }
        return flow;
    }
    void solve(int s, int t)
    {
        int flow = maxflow(s, t);
        cout << flow << endl;
    }
};
Dinic a;
int main()
{
    ios::sync_with_stdio(false);
    while(cin >> n >> m)
    {
        swap(n, m);
        a.init(n);
        for(int i = 0, x, y, z; i < m; i++)
        {
            cin >> x >> y >> z;
            a.add(x, y, z);
        }
        a.solve(1, n);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/head_hard/article/details/80216183