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

本文介绍了一种解决网络流问题的方法——快速网络流Dinic算法,并通过一个具体实例展示了如何利用该算法来求解最大流问题。文章提供了完整的AC代码实现。

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值