HDU 1532 Drainage Ditches(最大流)

最大流的三种模板

以HDU 1532 Drainage Ditches为例

          Drainage Ditches

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


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
 

 

Source
 

 

Recommend
lwg

 

1.Dinic算法

#include<iostream>
using namespace std;
const int maxn=150000;
const int maxm=2000000;
const int inf=1<<30;
struct edge
{
    int ,to,val,next;
}e[maxm];
int v[maxn],que[maxn],dis[maxn],len;
void init()
{
    len=0;
    memset(v,-1sizeof(v));
}
voidintint to,int va)
{
    e[len].=,e[len].to=to;
    e[len].val=va;
    e[len].next=v[];
    v[]=len++;
    e[len].=to,e[len].to=;e[len].val=0;
    e[len].next=v[to];v[to]=len++;
}
int Dinic(int n,int s,int t)
{
    int ans=0;
    whiletrue)
    {
        int head,tail,id,i;
        head=tail=0;que[tail++]=s;
        memset(dis,-1sizeof(dis));
        dis[s]=0;
        while(head<tail)
        {
            id=v[que[head++]];
            while(id!=-1)
            {
                if(e[id].val>0&&dis[e[id].to]==-1)
                {
                    dis[e[id].to]=dis[e[id].]+1;
                    que[tail++]=e[id].to;
                    if(e[id].to==t)
                    {
                        head=tail;
                        break;
                    }
                }
                id=e[id].next;
            }

        }
        if(dis[t]==-1break;
        id=s,tail=0;
        whiletrue)
        {
            if(id==t)
            {
                int flow=inf,fir;
                for(i=0;i<tail;i++if(e[que[i]].val<flow)
                    {
                        fir=i;
                        flow=e[que[i]].val;
                    }
                    for(i=0;i<tail;i++)
                e[que[i]].val-=flow,e[que[i]^1].val+=flow;
                ans+=flow;
                tail=fir;
                id=e[que[fir]].;
            }
            id=v[id];
            while(id!=-1)
            {
                if(e[id].val>0&&dis[e[id].]+1==dis[e[id].to])
                    break;
                id=e[id].next;
            }
            if(id!=-1)
            {
                que[tail++]=id;
                id=e[id].to;
            }
            else
            {
                if(tail==0break;
                dis[e[que[tail-1]].to]=-1;
                id=e[que[--tail]].;

            }
        }
    }
    return ans;
}
int main()
{
    int s,end,ca;
    int n,m;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        init();
        forint i=0;i<m;i++)
        {
            scanf("%d%d%d",&s,&end,&ca);
            (s-1,end-1,ca);   //重视下标
        }
        cout<<Dinic(n,0,n-1)<<endl;
    }
    return 0;
}

 

2.

#include<iostream>
using namespace std;
int n,m;
int c[201][201];
int maxflow(int s,int t)
{
    int p,q,queue[201],u,v,pre[201];
    int flow,aug;
    flow=0;
    whiletrue)
    {
        memset(pre,-1sizeof(pre));
        for(queue[p=q=0]=s;p<=q;p++)
        {
            u=queue[p];
        
        for(v=0;v<n&&pre[t]<0;v++if(c[u][v]>0&&pre[v]<0)
                pre[v]=u,queue[++q]=v;
            if(pre[t]>=0break;
        }
    
    if(pre[t]<0break;
    aug=0x7fff;
    for(u=pre[v=t];v!=s;v=u,u=pre[u])
        if(c[u][v]<aug)
            aug=c[u][v];
        for(u=pre[v=t];v!=s;v=u,u=pre[u])
            c[u][v]-=aug,c[v][u]+=aug;
        flow+=aug;
    }
    return flow;
}
int main()
{
    int s,e,ca;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(c,0sizeof(c));
        forint i=0;i<m;i++)
        {
            scanf("%d%d%d",&s,&e,&ca);
            c[s-1][e-1]+=ca;
        }
        printf("%d\n",maxflow(0,n-1));
    }
    return 0;
}

 

 

3.Edmonds_Karp算法

#include <iostream>
using namespace std;
#include <queue>
#define min(x,y) (x<y?x:y)
const int inf = 1000000000;
const int maxn = 210;
int cap[maxn][maxn];
int n, m;//n为边,m为顶点
int s, t;
int flow[maxn][maxn], d[maxn], parent[maxn], maxflow;

void Edmonds_Karp(int s,int t,int nnum)
{
    maxflow = 0;
    memset(flow, 0sizeof(flow));
    memset(parent, 0sizeof(parent));
    whiletrue)
    {
        memset(d, 0sizeof(d));
        queue<int>que;
        d[s]=inf;
        que.push(s);
        while(!que.empty())              //BFS寻找增广路
        {
            int u = que.front();
            que.pop();
            forint v=0; v<nnum; ++v)
            {
                if(!d[v] && cap[u][v]>flow[u][v])            //找到新结点v
                {
                    parent[v] = u;                          //记录v的父亲,并参加FIFO队列
                    que.push(v);
                    d[v] = min(d[u], cap[u][v]-flow[u][v]);//s-v路径上的最小残量
                }
            }
        }
        if0==d[t])                    //找不到,则当前流已经是最大流
            break;
        forint u=t;u!=s;u=parent[u])  //从汇点往回走
        {
            flow[parent[u]][u]+=d[t];  //更新正向流
            flow[u][parent[u]]-=d[t];  //更新反向流
        }
        maxflow += d[t];                 //更新从s流出的总流量
    }
}
int main()
{
    while(cin>>n>>m)
    {
        memset(cap,0sizeof(cap));
        while(n--)
        {
            int temps, tempe, c;
            cin>>temps>>tempe>>c;
            cap[temps-1][tempe-1]+=c;   //处理惩罚重边
        }
        Edmonds_Karp(0,m-1,m);
        cout<<maxflow<<endl;
    }
    return 0;
}

 

posted @ 2012-08-14 20:47  Suhx  阅读(176)  评论(0编辑  收藏  举报