hdu 1532 网络最大流

Drainage Ditches

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

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
 
 
 
 
 
 
各位,想学最大流的请无视这篇博客,本文只有ac代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<climits>
using namespace std;
int map[500][500];
int flow[500][500];
int a[500];
int pre[500];
int n,m;
int minn(int a,int b)
{
    return a<b?a:b;
}
int search(int s,int t)
{
    int maxn=0;
    memset(flow,0,sizeof(flow));
    while(1)
    {
        memset(a,0,sizeof(a));
        queue<int>q;
        a[s]=INT_MAX;
        q.push(s);
        int temp;
        while(!q.empty())
        {
            temp=q.front();
            q.pop();
            for(int i=1;i<=m;i++)
            {
                if(!a[i]&&flow[temp][i]<map[temp][i])
                {
                    pre[i]=temp;
                    a[i]=minn(a[temp],map[temp][i]-flow[temp][i]);
                    q.push(i);
                }
            }
        }
        if(a[t]==0)break;
        maxn+=a[t];
        for(int i=t;i!=s;i=pre[i])
        {
            flow[pre[i]][i]+=a[t];
            flow[i][pre[i]]-=a[t];
        }
    }
    return maxn;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int x,y,z;
        memset(map,0,sizeof(map));
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            map[x][y]+=z;
        }
        printf("%d
",search(1,m));
    }
}

 再贴个dinic算法的ac代码:

#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<queue>
#define INF 0x7fffffff
#define min(a,b) a<b?a:b
int N,M;
int level[205];
int Si,Ei,Ci;
struct Dinic
{
    int c;
    int f;
} edge[205][205];
bool dinic_bfs()//构造层次网络
{
    queue<int> Q;
    memset(level,0,sizeof(level));//初始化顶点的层次 为0
    Q.push(1);
    level[1]=1;
    int u,v;
    while(!Q.empty())
    {
        u=Q.front();
        Q.pop();
        for(v=1; v<=M; v++)
        {
            if(!level[v]&&edge[u][v].c>edge[u][v].f)//即顶点未被访问过,顶点u,v,存在边
            {
                level[v]=level[u]+1;//给顶点标记层次
                Q.push(v);
            }
        }
    }
    return level[M]!=0;//若返回false表明 汇点不在层次网络中
}
int dinic_dfs(int u,int cp)//进行增广
{
    int tmp=cp;
    int v,t;
    if(u==M)
        return cp;
    for(v=1; v<=M&&tmp; v++)
    {
        if(level[u]+1==level[v])
        {
            if(edge[u][v].c>edge[u][v].f)
            {
                t=dinic_dfs(v,min(tmp,edge[u][v].c-edge[u][v].f));
                edge[u][v].f+=t;
                edge[v][u].f-=t;
                tmp-=t;
            }
        }
    }
    return cp-tmp;
}
int dinic()//求出最大流
{
    int sum,tf;
    sum=tf=0;
    while(dinic_bfs())
    {
        while(tf=dinic_dfs(1,INF))
        {
            sum+=tf;
        }
    }
    return sum;
}
int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        memset(edge,0,sizeof(edge));
        while(N--)
        {
            scanf("%d%d%d",&Si,&Ei,&Ci);
            edge[Si][Ei].c+=Ci;//防止重边
        }
        int S=dinic();
        printf("%d
",S);
    }
    return 0;
}

 我写的dinic代码(我只想说这题够坑,输入那儿太猥琐了):

#include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#include<queue>
using namespace std;
struct Edge
{
    int s,t,c,next;
}edge[440];
int head[440];
int pre[220];
int n,m;
int Min(int a,int b)
{
    return a<b?a:b;
}
void init()
{
    int es,et,ec;
    int ent=0;
    memset(head,-1,sizeof(head));
    for(int i=0;i<n;i++)
    {
        int ok=1;
        scanf("%d%d%d",&es,&et,&ec);
        if(head[es]!=-1)
        {
            for(int i=head[es];i!=-1;i=edge[i].next)
            {
                if(edge[i].t==et)
                {
                    edge[i].c+=ec;
                    ok=0;
                    break;
                }
            }
        }
        if(ok){
        edge[ent].s=es;edge[ent].t=et;edge[ent].c=ec;edge[ent].next=head[es];head[es]=ent++;
        edge[ent].s=et;edge[ent].t=es;edge[ent].c=0;edge[ent].next=head[et];head[et]=ent++;
        }
    }
}
bool bfs()
{
    int temp;
    memset(pre,-1,sizeof(pre));
    queue<int>q;
    q.push(1);
    pre[1]=0;
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        for(int i=head[temp];i!=-1;i=edge[i].next)
        {
            int temp2=edge[i].t;
            if(pre[temp2]==-1&&edge[i].c)
            {
                pre[temp2]=pre[temp]+1;
                q.push(temp2);
            }
        }
    }
    return pre[m]!=-1;
}
int dfs(int s,int minn)
{
    int flow=0;
    if(s==m)return minn;
    for(int i=head[s];i!=-1;i=edge[i].next)
    {
        int temp=edge[i].t;
        if(pre[temp]==pre[s]+1&&edge[i].c)
        {
            int x=Min(minn-flow,edge[i].c);
            x=dfs(temp,x);
            edge[i].c-=x;
            edge[i^1].c+=x;
            flow+=x;
        }
    }
    return flow;
}
int dinic()
{
    int flow=0;
    while(bfs())
        flow+=dfs(1,INT_MAX);
    return flow;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        printf("%d
",dinic());
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lthb/p/4398402.html