Drainage Ditches POJ - 1273 (网络流初步)

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

纯网络流题,给定边数点数,源点汇点以及每条边的流量,求最大流。
这里用快速网络流dinic算法。
AC代码如下:

#include<iostream>
#include<cstring>
#include<string>
#include<deque>
#include<vector>
using namespace std;
const int N = 205;
const int inf = 0x3f3f3f3f;
int visit[N], layer[N];
int n, m;// 1代表源点,m代表汇点
int g[N][N];
bool layering() //进行分层
{
    deque<int> q;//双向队列
    memset(layer, -1, sizeof(layer));
    q.push_back(1);
    layer[1] = 0;
    while (!q.empty())
    {
        int front = q.front();
        q.pop_front();
        for (int i = 1; i <= m; i++)
        {
            if (layer[i] == -1 && g[front][i] > 0)
            {
                layer[i] = layer[front] + 1;
                if (i == m) return true;
                else    q.push_back(i);
            }
        }
    }
    return false;
}
int dinic()
{
    int maxflow = 0;
    deque<int> q;
    while (layering()) 
    {
        memset(visit, 0, sizeof(visit));
        q.push_back(1), visit[1] = 1;
        while (!q.empty())
        {
            int front = q.back();
            if (front == m) //如果该点是汇点
            {
                int minflow = inf, minflow_no;// 最小流以及最小流的起始点
                for (int i = 1; i < (int)q.size(); i++) //在双向队列里寻找
                {
                    int s = q[i - 1], e = q[i];
                    if (g[s][e] < minflow) minflow = g[s][e], minflow_no = s;
                }
                maxflow += minflow;
                for (int i = 1; i < (int)q.size(); i++) //添加反向边
                {
                    int s = q[i - 1], e = q[i];
                    g[s][e] -= minflow, g[e][s] += minflow;
                }
                while (!q.empty()&&q.back()!=minflow_no) //回溯到最小流的起始点或栈顶
                {
                    visit[q.back()] = 0;
                    q.pop_back();
                }
            }
            else  //如果不是汇点则继续向下寻找
            {
                int i;
                for ( i = 1; i <= m; i++)
                {
                    if (g[front][i] > 0 && layer[i] == layer[front] + 1 && !visit[i])
                    {
                        visit[i] = 1;
                        q.push_back(i);
                        break;
                    }
                }
                    if (i > m) //如果没找到则回溯
                    {
                        q.pop_back();
                    }

            }
        }
    }
    return maxflow;
}
int main()
{
    while (cin >> n >> m)
    {
        int s, e, c;
        memset(g, 0, sizeof(g));
        for (int i = 0; i < n; i++)
        {
            cin >> s >> e >> c;
            g[s][e] += c;
        }
        cout << dinic() << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinduo16/article/details/81663085