poj1273 Drainage Ditches

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 79977   Accepted: 31112

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

Source

题目大意:求1到n的最大流.
分析:网络流的模板题.
          第一次写网络流的题目,有几个地方需要注意一下:1.多组数据一定要清空邻接表(可以将同一组数据复制两边测是否清空好了) 2.bfs碰到终点返回true,每次走有流量并且没有走过的点. 3.dfs先判断有没有到达终点,扩展的点必须满足还有流量可以走到,并且刚好位于当前点的下一层,及时累加sum,如果sum等于流量了,及时返回. 4.如果扩展不了(sum == 0),这个点设为-1,以后不走了.
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int inf = 0x7fffffff,maxn = 1010;

int n,m,head[maxn],to[maxn],nextt[maxn],tot = 2,w[maxn],vis[maxn],ans;

void add(int x,int y,int z)
{
    w[tot] = z;
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;
}

bool bfs()
{
    queue <int> q;
    memset(vis,-1,sizeof(vis));
    vis[1] = 0;
    q.push(1);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        if (u == n)
            return true;
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (w[i] && vis[v] == -1)
            {
                vis[v] = vis[u] + 1;
                q.push(v);
            }
        }
    }
    return false;
}

int dfs(int u,int f)
{
    if (u == n)
        return f;
    int res = 0;
    for (int i = head[u];i;i = nextt[i])
    {
        int v = to[i];
        if (w[i] && vis[v] == vis[u] + 1)
        {
            int temp = dfs(v,min(f - res,w[i]));
            w[i] -= temp;
            w[i ^ 1] += temp;
            res += temp;
            if (res == f)
                return res;
        }
    }
    if (res == 0)
        vis[u] = -1;
    return res;
}

void dinic()
{
    while (bfs())
        ans += dfs(1,inf);
}

int main()
{
    while (scanf("%d%d",&m,&n) != EOF)
    {
        tot = 2;
        memset(head,0,sizeof(head));
        ans = 0;
        for (int i = 1; i <= m; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
            add(b,a,0);
        }
        dinic();
        printf("%d\n",ans);
    }

    return 0;
}

 

posted @ 2017-12-27 16:53  zbtrs  阅读(271)  评论(0编辑  收藏  举报