HDU 1532 Drainage Ditches 网络流模板

题目描述:

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

题目分析:

标准的最大流模板题。n条边,m个点,1点为源点,m点为汇点,跑一边最大流即可。

代码如下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

const int INF=0x3f3f3f3f;
const int MAXN=1010;
using namespace std;
struct node
{
    int v;
    int w;
    int next;
}mp[MAXN<<5];
int id;
int q[MAXN];
int level[MAXN];
int head[MAXN];

void init()
{
    memset(head,-1,sizeof(head));
    id=0;
}

void addedge(int u,int v,int w)
{
    mp[id].v=v;
    mp[id].w=w;
    mp[id].next=head[u];
    head[u]=id++;

    mp[id].v=u;
    mp[id].w=0;
    mp[id].next=head[v];
    head[v]=id++;
}

int bfs(int s, int t) //构建层次网络
{
    memset(level,0,sizeof(level));
    level[s]=1;
    int front=0,rear=1;
    q[front]=s;
    while(front < rear)
    {
        int x=q[front++];
        if(x==t) return 1;
        for(int e=head[x]; e!=-1; e=mp[e].next)
        {
            int v=mp[e].v, f=mp[e].w;
            if(!level[v] && f)
            {
                level[v]=level[x]+1;
                q[rear++]=v;
            }
        }
    }
    return 0;
}

int dfs(int u,int maxf,int t)
{
    if(u==t) return maxf;
    int ret=0;
    for(int e=head[u]; e!=-1; e=mp[e].next)
    {
        int v=mp[e].v, f=mp[e].w;
        if(level[u]+1==level[v] && f)
        {
            int Min=min(maxf-ret,f);
            f=dfs(v,Min,t);
            mp[e].w-=f;
            mp[e^1].w+=f;
            ret+=f;
            if(ret==maxf) return ret;
        }
    }
    return ret;
}

int dinic(int s, int t) //s源点 t汇点
{
    int ans = 0;
    while(bfs(s,t))
       ans+=dfs(s,INF,t);
    return ans;
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        init();
        for(int i=1; i<=n; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
        }
        int ans=dinic(1,m);
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(网络流)